Linear Regression
Linear Regression
clear all;
close all;
clc;
x = 1:7;
y = [0.5 2.5 2 4 3.5 6 5.5];
figure(1);
plot(x,y,'ro','LineWidth',1.5);
grid on;
hold on;
n = length(x);
xr = 1:0.01:7;
yr = a1*xr + a0;
figure(1);
plot(xr,yr,'b','LineWidth',1.5);
grid on;
hold on;
Polynomial Regression
clear all;
close all;
clc;
x = 0:5;
y = [2.1 7.7 13.6 27.2 40.9 61.1];
figure(1);
plot(x,y,'ro','LineWidth',1.5);
grid on;
hold on;
n = length(x);
X = [n sum(x) sum(x.^2);
sum(x) sum(x.^2) sum(x.^3);
sum(x.^2) sum(x.^3) sum(x.^4)];
A = X\Y;
A = A';
a0 = A(1);
a1 = A(2);
a2 = A(3);
xr = 0:0.01:5;
yr = a2*(xr.^2) + a1*xr + a0;
figure(1);
plot(xr,yr,'b','LineWidth',1.5);
grid on;
hold on;
Linear Interpolation
clear all;
close all;
clc;
x = 0:6; % x = 0 1 2 3 4 5 6
y = [0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794];
n = length(x); % n = 7
figure(1);
plot(x,y,'ro','LineWidth',1.5);
grid on;
hold on;
for i=1:L
for j=1:(n-1)
end
figure(1);
plot(xs,ys,'b--');
grid on;
hold on;
xp = 0.79;
tol = 0.001;
ind = find(abs(xs-xp)<=tol);
yp = ys(ind);
display(xp);
display(yp);
figure(1);
plot(xp,yp,'rx','LineWidth',2);
grid on;
hold on;
Newton R
clear all;
close all;
clc;
% Initial guess
x0 = 0;
% Tolerance
tol = 1e-8;
% Iteration counter
iter = 0;
% Newton-Raphson iteration
while abs(f(x_new)) > tol
x_new = x_new - f(x_new) / df(x_new);
iter = iter + 1;
end
Bisection
clear all;
close all;
clc;
False Position
clear all;
close all;
clc;
iter = iter + 1;
if iter > max_iter
fprintf('Maximum iterations reached.\n');
break;
end
end
exp5prb1
clear all;
close all;
clc;
% Initial guess
x0 = 0;
% Tolerance
tol = 1e-8;
% Iteration counter
iter = 0;
% Newton-Raphson iteration
while abs(f(x_new)) > tol
x_new = x_new - f(x_new) / df(x_new);
iter = iter + 1;
end
exp5prb2a
close all;
clear all;
clc;
% Data
x = [0 4 8 12 16 20];
y = [67 84 98 125 149 185];
% Display coefficients
fprintf('Linear Fit: y = %fx + %f\n', a1, a0);
exp5prb2b
close all;
clear all;
clc;
% Given data
x = [0 4 8 12 16 20];
y = [67 84 98 125 149 185];
Y = [sum(y);
sum(x .* y);
sum((x.^2) .* y)];
exp5prb3
close all;
clear all;
clc;
figure(1);
plot(x, y, 'o', 'MarkerFaceColor', 'b');
hold on;
grid on;
n = length(x);
A = X\Y;
A = A';
a0 = A(1);
a1 = A(2);
a2 = A(3);
% Given data
x = [0 1.8 5 6 8.2 9.2 12]; % x values
y = [26 16.415 5.375 3.5 2.015 2.54 8]; % y values
n = length(x);
% Interpolation range
xs = linspace(min(x), max(x), 500); % Densely sampled x values
ys = zeros(size(xs)); % Preallocate
% Interpolation at x = 3.5
x_target = 3.5;
for j = 1:(n-1)
if x_target >= x(j) && x_target <= x(j+1)
y_interp = y(j) + ((y(j+1) - y(j)) / (x(j+1) - x(j))) * (x_target -
x(j));
break;
end
end
% Display result
fprintf('Estimated y at x = %.2f: %.4f\n', x_target, y_interp);
% Given data
T = [0 8 16 24 32 40]; % Temperature in Celsius
O2 = [14.621 11.843 9.870 8.418 7.305 6.413]; % Oxygen concentration (mg/L)
n = length(T);
% Interpolation range
Ts = 0:0.1:40; % Dense temp range
L = length(Ts);
O2s = zeros(1, L);
% Estimate at T = 27°C
T_target = 27;
tol = 0.01;
ind = find(abs(Ts - T_target) <= tol);
O2_interp = O2s(ind);
% Display results
fprintf('Estimated Oxygen at T = %.1f°C: %.3f mg/L\n', T_target, O2_interp);
exp8prb1
clear all;
close all;
clc;
% Initial guess
x0 = 0;
% Tolerance
tol = 1e-8;
% Iteration counter
iter = 0;
% Newton-Raphson iteration
while abs(f(x_new)) > tol
x_new = x_new - f(x_new) / df(x_new);
iter = iter + 1;
end