saving regula-falsi iterations

1 回表示 (過去 30 日間)
dario cecchetti
dario cecchetti 2017 年 5 月 14 日
回答済み: Astarag Chattopadhyay 2017 年 5 月 17 日
Hi everybody, I would like to save the iterations of X variable into a vector. I can only get the final resul, that is the solution equation, while my goal is that to obtain the whole series.
ex: x=[x1,x2,...,xn].
function [x,iter]= RegulaFalsi(f,a,b,ep,itermax)
x0=a;
x1=b;
iter=0; %number iterations
x=b;
while (abs(f(x)) > ep) && (iter<itermax)
x=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0))); %intersection X axis
if (f(x)*f(x0)>0)
x0=x;
x1=b;
else (f(x)*f(b)>0)
x1=x;
x0=a;
end
if iter==itermax
disp('Maximum number of iterations reached')
end
iter=iter+1;
end

採用された回答

Astarag Chattopadhyay
Astarag Chattopadhyay 2017 年 5 月 17 日
Hi,
I would suggest you to use an array to store the value of x in every iteration. You can accomplish this using the following code snippet:
function [x,iter]= RegulaFalsi(f,a,b,ep,itermax)
x0=a;
x1=b;
iter=0; %number iterations
x=b;
i = 1;
x = zeros(1,itermax);
x(i)=b;
z=x(i);
while (abs(f(z)) > ep) && (iter<itermax)
x(i)=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0))); %intersection X axis
z=x(i);
i= i+1;
if (f(z)*f(x0)>0)
x0=z;
x1=b;
else (f(z)*f(b)>0)
x1=z;
x0=a;
end
iter=iter+1;
if iter==itermax
disp('Maximum number of iterations reached')
end
end
end
Another alternative would be to make use of the following code for "Regula-Falsi Method" provided in MATLAB Central:

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by