Help storing data from while loop

1 回表示 (過去 30 日間)
John Furman
John Furman 2020 年 2 月 28 日
回答済み: Walter Roberson 2020 年 2 月 28 日
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

採用された回答

Walter Roberson
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 件)

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by