clc
clear
close all
% Input and output data
V = (-100:5:-65)';
Iss = [-10; -8; -7.5; -6.5; -4; -2; 0.5; 7];
% Design matrix for the first-degree model: I = theta0 + theta1*V
X = [ones(size(V)), V];
% Least-squares parameter estimation
theta_hat = X \ Iss;
% Estimated current and mean squared error
Iss_hat = X * theta_hat;
MSE = mean((Iss - Iss_hat).^2);
% Display parameters and error
fprintf('theta0 = %.6f\n', theta_hat(1))
fprintf('theta1 = %.6f\n', theta_hat(2))
fprintf('MSE = %.6f\n', MSE)
% Generate points for plotting the fitted line
x_plot = (-100:0.02:-65)';
y_plot = theta_hat(1) + theta_hat(2) * x_plot;
% Plot measured data and fitted model
figure
plot(V, Iss, 'ro', 'MarkerFaceColor', 'r')
hold on
plot(x_plot, y_plot, 'b-', 'LineWidth', 1.5)
grid on
xlabel('Voltage (V)')
ylabel('Current (I)')
title('First-Degree Model')
legend('Measured Data', 'Fitted Model', 'Location', 'northeast')
To embed this project on your website, copy the following code and paste it into your website's HTML: