Please solve the following in MATLAB Please add the comments
Solution
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

