How can I create a table containing all iterations?

5 ビュー (過去 30 日間)
Emily
Emily 2020 年 9 月 13 日
回答済み: Ayush Gupta 2020 年 9 月 17 日
I need to have a table containing iter, xr, func(xr), and es for each iteration of the loop. I have tried listing them as arrays but cannot seem to get that to work.
function[root,ea,iter]=secant(func,delta,xr,es,maxit,varargin)
if nargin<3,error('atleast 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter=0;
while (1)
xrold=xr;
xr=xr-((delta*xr*func(xr))/(func(xr+(delta*xr))-func(xr)));
iter=iter+1;
if xr~=0, ea=abs((xr-xrold)/xr)*100;end
if ea<=es|iter>=maxit,break,end
funcxr=func(xr);
end
root=xr;

採用された回答

Ayush Gupta
Ayush Gupta 2020 年 9 月 17 日
To create a table, store every instance of iter, xr, func(xr), and es at each iteration into a list that is globally and append each iteration to it. Refer to the following code on how to do it:
function[root,ea,iter]=secant(func,delta,xr,es,maxit,varargin)
if nargin<3,error('atleast 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
itr =[];
xr =[];
func_xr =[];
es =[];
iter=0;
while (1)
xrold=xr;
xr=xr-((delta*xr*func(xr))/(func(xr+(delta*xr))-func(xr)));
iter=iter+1;
if xr~=0, ea=abs((xr-xrold)/xr)*100;end
if ea<=es|iter>=maxit,break,end
funcxr=func(xr);
itr(i) = iter;
xr(i) = xr;
func_xr(i) = funcxr;
es(i) = es;
end

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by