Assignement C
Assignement C
clc;
clear;
% Parameters
fprintf('--------------------------\n');
for i = 1:max_iteration
break;
end
x0 = x1;
x1 = x2;
end
Gauss:
clc;
clear;
close all;
% Initial guesses
x0 = 0;
y0 = 0;
z0 = 0;
% Tolerance
tol = 1e-4;
maxIter = 100;
for i = 1:maxIter
w(i)=x;
y = (1/10)*(7 - 2*x- 3*z0);
f(i)=y;
z = (1/10)*(6 - x - 2*y);
d(i)=z;
% Convergence check
if abs(x - x0) < tol && abs(y - y0) < tol && abs(z - z0) < tol
break;
end
y0=y;
z0=z;
x0=x;
end
plot(w);hold on
plot(f);hold on
plot(d);hold on
Jacobi:
% Initial guesses
x0 = 0;
y0 = 0;
z0 = 0;
% Tolerance
tol = 1e-4;
maxIter = 100;
for i = 1:maxIter
x = (1/4)*(4 - y0 - z0);
z = (1/3)*(3 - x0 - y0);
% Display current iteration
% Convergence check
if abs(x - x0) < tol && abs(y - y0) < tol && abs(z - z0) < tol
break;
end
% Update guesses
x0 = x;
y0 = y;
z0 = z;
end
clc;
clear;
% Initial interval
a = 3;
b = 4;
tol = 1e-6;
% Check that a root exists in [a, b]
if f(a)*f(b) >= 0
end
x0 = zeros(1, 20);
fprintf('--------------------------\n');
for i = 1:20
break;
end
if f(a)*f(x0(i)) < 0
b = x0(i);
else
a = x0(i);
end
end
% Final result
% Plotting convergence
figure;
xlabel('Iteration');
ylabel('Approximate Root');
grid on;
Bisection Mehtod:
clc
clear all
close all
f = @(x) exp(-x)*(3.2*sin(x)-0.5*cos(x));
a = 3;
b = 4;
tol = 0.0001;
if (f(a)*f(b)) < 0
for i = 1:17
x0 = (a+b)/2 ;
if (f(a)*f(x0)) < 0
b = x0;
a = x0;
end
break;
end
end
end
Newton Rapshon:
clc;
clear;
% Initial guess
x0 = 3.5;
tol = 1e-6;
max_iter = 20;
fprintf('Newton-Raphson Iterations:\n');
fprintf('---------------------------\n');
for i = 1:max_iter
break;
end
end