How do I save the outputs from my 'while loop' into a vector?
显示 更早的评论
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!
回答(1 个)
Azzi Abdelmalek
2013-2-27
编辑:Azzi Abdelmalek
2013-2-27
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
类别
在 帮助中心 和 File Exchange 中查找有关 Semiconductors and Converters 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!