0% found this document useful (0 votes)
7 views6 pages

University of Tripoli Faculty of Engineering Electrical and Electronics Engineering Department MATLAB Assignment EE303

Uploaded by

alharithmilad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

University of Tripoli Faculty of Engineering Electrical and Electronics Engineering Department MATLAB Assignment EE303

Uploaded by

alharithmilad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

University of Tripoli

Faculty of Engineering
Electrical and Electronics Engineering Department
MATLAB assignment

EE303

Name: ‫الحارث توفيق الغصري‬


Student:2190203468
Serial No.19
First: False position method:
The code is:
% Prompt the user for the function to find the root of
f_string = input('Enter the function to find the root of:
','s');
f = str2func(['@(x)' f_string]);

% Prompt the user for the interval [a,b]


a = input('Enter the left endpoint of the interval: ');
b = input('Enter the right endpoint of the interval: ');

% Prompt the user for the maximum number of iterations and the
tolerance
max_iterations = input('Enter the maximum number of iterations:
');
tolerance = input('Enter the tolerance: ');

% Initialize the iteration counter


iteration = 1;

% Perform false position method until convergence or max


iterations
while iteration <= max_iterations
% Calculate the function values at a and b
fa = f(a);
fb = f(b);

% Calculate the interpolated point c


c = b - fb*(b-a)/(fb-fa);

% Calculate the function value at c


fc = f(c);

% Check for convergence


if abs(fc) < tolerance
fprintf('Converged to root %f in %d iterations.\n', c,
iteration);
break;
end

% Update the interval [a, b]


if fa*fc < 0
b = c;
else
a = c;
end

% Update the iteration counter


iteration = iteration + 1;
end

% Check for non-convergence


if iteration > max_iterations
fprintf('Method did not converge in %d iterations.\n',
max_iterations);
end

u can put any function using a and b like


a+2b
u can even put the max iterations u want and the tolerance.

Second: Indirect Jacobi Method:


The code is:
% Prompt the user for the coefficient matrix A, the right-hand
side vector b,
% and the initial guess x0
A = input('Enter the coefficient matrix A: ');
b = input('Enter the right-hand side vector b: ');
x0 = input('Enter the initial guess x0: ');

% Prompt the user for the maximum number of iterations and the
tolerance
max_iterations = input('Enter the maximum number of iterations:
');
tolerance = input('Enter the tolerance: ');

% Extract the diagonal of A


D = diag(diag(A));

% Extract the off-diagonal part of A


L = tril(A) - D;
U = triu(A) - D;

% Initialize the iteration counter and the current approximation


x
iteration = 1;
x = x0;

% Perform Jacobi iteration until convergence or max iterations


while iteration <= max_iterations
% Calculate the new approximation x_new using the indirect
Jacobi formula
x_new = D \ (b - (L+U)*x);

% Check for convergence


if norm(x_new - x) < tolerance
fprintf('Converged to solution x =\n');
disp(x_new);
fprintf('in %d iterations.\n', iteration);
break;
end

% Update the iteration counter and the current approximation


x
iteration = iteration + 1;
x = x_new;
end

% Check for non-convergence


if iteration > max_iterations
fprintf('Method did not converge in %d iterations.\n',
max_iterations);
end

In this code, the user need to enter the coefficient matrix A, the right-
hand side vector b, and the initial guess x0. We also prompt the user to
enter the maximum number of iterations and the tolerance.
You can enter any coefficient matrix, right-hand side vector, initial
guess, maximum number of iterations, and tolerance that you want to
solve the system of linear equations using the indirect Jacobi method.

Second: Simpson 1/3 :


The code is:
% Prompt the user for the function f(x), the integration limits
a and b,
% and the number of subintervals n (which must be even)
f = input('Enter the function f(x) as a string: ','s');
a = input('Enter the lower limit of integration: ');
b = input('Enter the upper limit of integration: ');
n = input('Enter the number of subintervals (must be even): ');

% Convert the function string to a function handle


f_handle = str2func(['@(x) ' f]);

% Calculate the width of each subinterval


h = (b - a) / n;

% Evaluate the function at the endpoints and midpoints of the


subintervals
x = linspace(a, b, n+1);
y = f_handle(x);
y_mid = f_handle((x(1:end-1) + x(2:end)) / 2);

% Apply Simpson's 1/3 rule to approximate the integral


integral = (h/3) * (y(1) + 4*sum(y_mid) + 2*sum(y(2:end-1)) +
y(end));

% Display the result


fprintf('Approximate value of the integral: %f\n', integral);
this is an example to how this code work:
Enter the function f(x) as a string: 'x.^2'
Enter the lower limit of integration: 0
Enter the upper limit of integration: 1
Enter the number of subintervals (must be even): 4
The code will then calculate the approximate value of the
integral using Simpson's 1/3 rule and display the result as:
Approximate value of the integral: 0.333333

You might also like