Please solve the following in MATLAB. Please add the comments in each line and p
ID: 2316309 • Letter: P
Question
Please solve the following in MATLAB. Please add the comments in each line and provide the coding, window and the command window, try to make the codes as universal as possible. Thank you!! |Create a function file that performs fixed-point iteration to find the squareroot s of a function. Test your code for the following functions. Your inputs should be the g formula, its derivative, an initial guess, and tolerance. Your code must also take convergence into consideration. X^3 - x^2 = x cos(x) = xExplanation / Answer
for x3-x2=x
%clearing window and variables
clear all;
close all;
clf;
clc;
fh = @(x)x^3-x^2-x; %The function
tolerance=.01;
fd= @(x)3*x^2-2*x-1; %The derivative
x0=-1; %THE INITIAL VALUE;
dx=-fh(x0)/fd(x0);
x1=x0+dx;
e=fh(x1)-fh(x0);
while(abs(e)>tolerance)
x0=x1;
dx=-fh(x0)/fd(x0);
x1=x0+dx;
e=fh(x1)-fh(x0);
end
if(x1==inf) %checking convergence
fprintf(' Solution not converged');
end
x1 %Result
Result;
x1 =
-0.6180
%clearing window and variables
clear all;
close all;
clf;
clc;
fh = @(x)cos(x)-x; %The function
tolerance=.01;
fd= @(x)-sin(x)-1; %The derivative
x0=-1; %THE INITIAL VALUE;
dx=-fh(x0)/fd(x0);
x1=x0+dx;
e=fh(x1)-fh(x0);
while(abs(e)>tolerance)
x0=x1;
dx=-fh(x0)/fd(x0);
x1=x0+dx;
e=fh(x1)-fh(x0);
end
if(x1==inf) %checking convergence
fprintf(' Solution not converged');
end
x1 %Result
Result;
Solution not converged
x1 =
Inf
for cos(x)=x
%clearing window and variables
clear all;
close all;
clf;
clc;
fh = @(x)cos(x)-x; %The function
tolerance=.01;
fd= @(x)-sin(x)-1; %The derivative
x0=-1; %THE INITIAL VALUE;
dx=-fh(x0)/fd(x0);
x1=x0+dx;
e=fh(x1)-fh(x0);
while(abs(e)>tolerance)
x0=x1;
dx=-fh(x0)/fd(x0);
x1=x0+dx;
e=fh(x1)-fh(x0);
end
if(x1==inf) %checking convergence
fprintf(' Solution not converged');
end
x1 %Result
Result
x1 =
0.7391