Need help to debug code

1 次查看(过去 30 天)
Cassandra Soliman
function [x1] = tutorial1(x0,nMax,tol)
x0 = 1.5;
nMax = 15;
tol = 1e-4;
% for loop acting continously for 15 iterations
for i = 1:nMax
fx0= (x0).^3-3.^(x0)+1;
differentialx0=3.*(x0.^2) - 3.^x0.*log(3);
%function f(x0)respectively
x1 = x0 -fx0./differentialx0;
if abs(x1-x0)<tol
break
end
x0 = x1;
fprintf('Iteration = %d, x0 = %.4f, x1 = %.4f, fx1 = %.4f\n',i,x0,x1);
end
% Sample output code for monitoring (this should be included in your loop structure.
return

回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020-3-2
编辑:KALYAN ACHARJYA 2020-3-2
#Need help to debug code
There is no coding error.
function [x1]=tutorial1(x0,nMax,tol)
% for loop acting continously for 15 iterations
for i = 1:nMax
fx0= (x0).^3-3.^(x0)+1;
differentialx0=3.*(x0.^2) - 3.^x0.*log(3);
%function f(x0)respectively
x1 = x0 -fx0./differentialx0;
if abs(x1-x0)<tol
break
end
x0 = x1;
fprintf('Iteration = %d, x0 = %.4f, x1 = %.4f, fx1 = %.4f\n',i,x0,x1);
end
% Your code as per definition/requirements
end
Save the function as Matlab function file "tutorial1.m" and call it from command promt or a other main script. Please note during call you have to pass the input arguments, as specified in the function file.
>> y=tutorial1(1.5,15,1e-4)

类别

Help CenterFile Exchange 中查找有关 Labels and Annotations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by