how to store value from loop

3 ビュー (過去 30 日間)
sazzad hossen
sazzad hossen 2014 年 5 月 26 日
コメント済み: sazzad hossen 2014 年 5 月 27 日
for i=1:0.1:5
ly=i;
for j=1:0.1:5;
lx=j;
c1=co1(x1,y1,z1,lx,ly,lz,f);
c2=co2(x2,y2,z1,lx,ly,lz,f);
c3=co3(x3,y3,z1,lx,ly,lz,f);
%options = optimoptions('fsolve','Display','iter');
func = @(x)efunction(x,x1,x2,x3,y1,y2,y3,c1,c2,c3,f);
[x,fval] = fsolve(func,x0);
X=x(1,1);
Y=x(1,2);
H=x(1,3);
x_error= sprintf('%.10f',abs(X-lx));
y_error=sprintf('%.10f',abs(Y-ly));
z_error=sprintf('%.10f',abs((z1-H)-lz));
end
end
problem : i just get only one value in each parameter of x_error, y_error, z_error
but i want to get/store all value of x_error, y_error and z_error

採用された回答

rifat
rifat 2014 年 5 月 27 日
Your index isnt integer.. Use the following modification. And your program isnt self sufficient for me to execute.
idx = 0;
for i=1:0.1:5
ly=i;
idx=idx+1;
idy=0;
for j=1:0.1:5;
idy=idy+1;
lx=j;
c1=co1(x1,y1,z1,lx,ly,lz,f);
c2=co2(x2,y2,z1,lx,ly,lz,f);
c3=co3(x3,y3,z1,lx,ly,lz,f);
%options = optimoptions('fsolve','Display','iter');
func = @(x)efunction(x,x1,x2,x3,y1,y2,y3,c1,c2,c3,f);
[x,fval] = fsolve(func,x0);
X=x(1,1);
Y=x(1,2);
H=x(1,3);
x_error{idx,idy}= sprintf('%.10f',abs(X-lx));
y_error{idx,idy}=sprintf('%.10f',abs(Y-ly));
z_error{idx,idy}=sprintf('%.10f',abs((z1-H)-lz));
end
end
  1 件のコメント
sazzad hossen
sazzad hossen 2014 年 5 月 27 日
thanks for your help

サインインしてコメントする。

その他の回答 (1 件)

Sara
Sara 2014 年 5 月 26 日
Define a cell array before the loop:
x_error = cell(50,50); % Adjust num elements according to your loops
and then do:
x_error{i,j} = ....
The same for the other variables.
  1 件のコメント
sazzad hossen
sazzad hossen 2014 年 5 月 27 日
it's not wotk
x_error = cell(1681,1); % because of two loop 41*41=1681
x_error = cell(1681,1);
x_error = cell(1681,1);
for i=1:0.1:5 ly=i;
for j=1:0.1:5;
lx=j;
c1=co1(x1,y1,z1,lx,ly,lz,f);
c2=co2(x2,y2,z1,lx,ly,lz,f);
c3=co3(x3,y3,z1,lx,ly,lz,f);
%options = optimoptions('fsolve','Display','iter');
func = @(x)efunction(x,x1,x2,x3,y1,y2,y3,c1,c2,c3,f);
[x,fval] = fsolve(func,x0);
X=x(1,1);
Y=x(1,2);
H=x(1,3);
x_error{i,j}= sprintf('%.10f',abs(X-lx));
y_error{i,j}=sprintf('%.10f',abs(Y-ly));
z_error{i,j}=sprintf('%.10f',abs((z1-H)-lz));
end
end
error result:
Subscript indices must either be real positive integers or logicals.
Error in solution2 (line 39) x_error{i,j}= sprintf('%.10f',abs(X-lx));

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGlobal or Multiple Starting Point Search についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by