Numerical methods (matlab) Q1 The goal is to find the root of 0.3 x /3 1.4 using
ID: 3800370 • Letter: N
Question
Numerical methods (matlab)
Explanation / Answer
The function fixed_point.m
function [x,N] = fixed_point(g,x0,tol,kmax)
N = 1; % iteration variable
x = g(x0); % Evaluating g at x0
while((abs(x0-x)> tol)&&(N<=kmax))
x0 = x;% copy the value of new x to old x
x = g(x0);% Evaluating g at x0
N =N+1; % increaseing the number of iterations
end
N= N-1;
end
Testing the function part a) and part b)
g = @(x) 0.3*x.^2-(nthroot(x,3))-1.4; % The function g
g1 = @(x) (nthroot(x,3)+1.4)./(0.3*x); % The function g1
g2 = @(x) (0.3*x.^2-1.4).^3; % the function g2
x = -3:0.1:5; % X vector for the plot
plot(x,g(x),[-3 5],[0 0]); % plotting thye function g to graphicallyt locate
tol = 10^-5; % tolerence
kmax = 100; % Max iterations
x0 = 2; % left point
[x,N] = fixed_point(g1,x0,tol,kmax);
fprintf(' Root = %f for x0 = %f Number of iterations %d ',x,x0,N);
x0 = 4; % Right point
[x,N] = fixed_point(g1,x0,tol,kmax);
fprintf(' Root = %f for x0 = %f number of iterations %d ',x,x0,N);
x0 = -4; % left point
[x,N] = fixed_point(g2,x0,tol,kmax);
fprintf(' Root = %f for x0 = %f Number of iterations %d ',x,x0,N);
x0 = -1; % Right point
[x,N] = fixed_point(g2,x0,tol,kmax);
fprintf(' Root = %f for x0 = %f Number of iterations %d ',x,x0,N);
OUTPUT
>> fixed_point_test
Root = 3.085336 for x0 = 2.000000 Number of iterations 67
Root = 3.085336 for x0 = 4.000000 number of iterations 64
Root = Inf for x0 = -4.000000 Number of iterations 5
Root = -2.743999 for x0 = -1.000000 Number of iterations 100
Remarks
g2 is faild to converge to the root
Not in problem just Testing the code using fzero() fun
x0 = fzero(g,3); % the zero point
[x,N] = fixed_point(g2,x0,tol,kmax);
fprintf(' Root = %f for x0 = %f Number of iterations %d ',x,x0,N);
x0 = fzero(g,3)+0.1; % Right point
[x,N] = fixed_point(g2,x0,tol,kmax);
fprintf(' Root = %f for x0 = %f Number of iterations %d ',x,x0,N);
x0 = fzero(g,3)-0.1; % left point
[x,N] = fixed_point(g2,x0,tol,kmax);
fprintf(' Root = %f for x0 = %f Number of iterations %d ',x,x0,N);
OUTPUT
Root = 3.085340 for x0 = 3.085340 Number of iterations 0
Root = Inf for x0 = 3.185340 Number of iterations 5
Root = -2.095148 for x0 = 2.985340 Number of iterations 100
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.