Help storing data from while loop
1 回表示 (過去 30 日間)
古いコメントを表示
I am working on an assignment where I need to save the iteration and the root value for each iteration under a single column matrix I am going to put my code underneath. Please let me know if you can help!
x = [0:0.1:2];
% Set bounds for graph
F = 5*sin(x/4)-1.3;
% Create function for graph
plot(x,F),grid
% Display function
[bracket,x] = ginput(2)
% Let user select two bounds from graph
x_lo = min(bracket)
x_up = max(bracket)
f = @(x) 5*sin(x/4) - 1.3
error = 10;
iter = 0
while error > 1e-4
iter = iter+1
tr = x_lo - f(x_lo)*(x_up-x_lo)/(f(x_up) - f(x_lo))
fr = f(tr)
if sign(fr) == sign(f(x_up))
x_up = tr
else
x_lo = tr
end
error = abs(fr);
end
root = tr
0 件のコメント
採用された回答
Walter Roberson
2020 年 2 月 28 日
info_to_save(iter, :) = [iter, fr];
However, since you know that the iterations always increase by 1 each time, you do not need to store the iteration number:
info_to_save(iter) = fr;
And afterwards:
[(1:length(info_to_save)).', info_to_save(:)]
0 件のコメント
その他の回答 (0 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!