How do I save the outputs from my 'while loop' into a vector?
Show older comments
Here is my function file:
f=inline('x^3+1/x^2+4*sqrt(x)-10')
x0=input('Input your first guess: ')
relErr=1;
while relErr>0.0005
m1=(f(x0+.001)-f(x0))/.001;
m=x0-(f(x0)/m1);
relErr=abs(m-x0)/abs(x0);
fprintf('%-6.4f ',m)
x0=m;
end
I need to save the outputs for 'm' into a vector called 'Root', and I need to save the outputs for 'relErr' into a vecotr called 'Error'.
Thanks!
Answers (1)
Azzi Abdelmalek
on 27 Feb 2013
Edited: Azzi Abdelmalek
on 27 Feb 2013
f=inline('x^3+1/x^2+4*sqrt(x)-10')
x0=input('Input your first guess: ')
relErr=1;
m=[ ];
relErr=[ ];
while relErr>0.0005
m1=(f(x0+.001)-f(x0))/.001;
m(end+1)= x0-(f(x0)/m1);
relErr(end+1)= abs(m-x0)/abs(x0);
fprintf('%-6.4f ',m)
x0=m(end+1);
end
Categories
Find more on Semiconductors and Converters in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!