Help storing data from while loop

3 Ansichten (letzte 30 Tage)
John Furman
John Furman am 28 Feb. 2020
Beantwortet: Walter Roberson am 28 Feb. 2020
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 28 Feb. 2020
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(:)]

Weitere Antworten (0)

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by