Need help to debug code
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Antworten (1)
KALYAN ACHARJYA
am 2 Mär. 2020
Bearbeitet: KALYAN ACHARJYA
am 2 Mär. 2020
#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)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!