Saving vector result from each loop if solution is correct.
显示 更早的评论
Let's say DMP.m uses fsolve and output a solution vector, named xx. It can be `Equation solved', 'No solution found', or 'Equation solved but inaccuracy possible.'
I want to store the vector if it tells `Equation solved'.
So for example, my code is
for r=1:10
DMP
if exitflag==1
xxx(r,:)=xx
end
end
Unrecognized function or variable 'r'.
But Matlab gives me the error message: Unrecognized function or variable 'r'.
Could you help me the coding?
采纳的回答
14 个评论
My pleasure!
.
Why Matlab says "Unrecognized function or variable 'r'."?
I am not certain what is throwing that error. I created my own version of ‘DMP’ to test it, and it appears to work correctly as originally written (with my ‘DMP’ code, at least) —
for r=1:10
[xx,exitflag] = DMP(r);
if exitflag==1
xxx(r,:)=xx;
end
end
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 0.6180
fv = 3.2817e-08
exitflag = 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 1.0000
fv = 1.0658e-13
exitflag = 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 1.3028
fv = 1.7319e-13
exitflag = 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 1.5616
fv = 2.2162e-08
exitflag = 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 1.7913
fv = 2.7710e-10
exitflag = 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 2.0000
fv = 7.5939e-13
exitflag = 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 2.1926
fv = 3.4635e-11
exitflag = 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 2.3723
fv = 5.3424e-08
exitflag = 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 2.5414
fv = 7.0818e-09
exitflag = 1
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
xx = 2.7016
fv = 6.7687e-08
exitflag = 1
xxx
xxx = 10×1
0.6180
1.0000
1.3028
1.5616
1.7913
2.0000
2.1926
2.3723
2.5414
2.7016
function [xx,exitflag] = DMP(r)
[xx,fv,exitflag] = fsolve(@(x)x.^2+x - r, rand)
end
I only see the posted code and nothing else, so what is not posted may be causing the problem.
.
Oh.. great thank you so much.
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
I changed r to i then the problem disappeard.
There is another problem, however. Matlab says "Index in position 1 is invalid. Array indices must be positive integers or logical values."
clear
for i=1:10
DMP;
if exitflag==1
xxx(i,:)=xx;
end
end
I am not certain what MATLAB version/release is being used, however in older releases, ‘i’ and ‘j’ when used alone were considered the imaginary operator
, so here ‘i’ is not a real number. Either substitute it for another variable name or use ‘ii’. Then it will not be confused with the imaginary operator.
, so here ‘i’ is not a real number. Either substitute it for another variable name or use ‘ii’. Then it will not be confused with the imaginary operator. .
When I change i to k, it gives me the previous error message: "Unrecognized function or variable 'k'."
By the way, xx is a vector: xx = 0.0411 0.1280 0.0153 24.3166 0.0323 0.0067
for k=0:10
DMP;
if exitflag==1
xxx(k,:)=xx;
end
end
I suspect that ‘k’ may be being used int the ‘DMP’ function, however is not being passed to it as an argument, and that throws the error. Since I have never seen the ‘DMP’ function (except the one I wrote to test the code that worked correctly) or the reference to it in the posted code (note that the ‘DMP’ call alone as presented in the posted code will run it, however will not pass anything to it or return anything from it), that is only a guess.
My test code sends ‘r’ to it as an argument and returns the necessary information from it in the function call to it. It shows the correct way to call ‘DMP’ with its arguments, and return values from it to the calling script, so they can be used approporiately.
.
Thank you for your detailed answer. Here is the link for my entire code in case you can see it. Anyway, you helped me so much already.
Note that ‘DPM’ is a script, not a function, so it will not return anything. I changed it to be a function here.
Try this version (it takes too long to run it here, so I ran it offline on my computer) —
for r=1:10
[xx,exitflag] = DMP;
if exitflag==1
xxx(r,:)=xx;
end
end
xxx
function F = equations(x,beta,w,p,z,b,d)
u=x(1);
v=x(2);
theta=x(3);
eta=x(4);
a=x(5);
lambda=x(6);
c=x(7);
F(1)=((abs(lambda)+b)*(1-u)*u)/((abs(lambda)+b)*(u-1)+100*a*u)-v;
F(2)=p-(p*c)/(beta*(100*a/(1+theta)))*(1-beta*(1-d)*(1-abs(lambda)))-w;
F(3)=abs(eta)*p+(1-abs(eta))*z+abs(eta)*theta*p*c*(1-d)-w;
F(4)=v/u-theta;
F(5)=-.3697544+.0341093/abs(eta)+7.533068/c-theta;
F(6)=-.0410965+3.417642*abs(d)-abs(lambda);
F(7)=66.2881*abs(eta)-1061.607*lambda+693.8852*a+3.338711*p-4.223466*w-c;
F(8)=-.0162202/c/eta+.0009784*c+.0016295/eta+1.396799*lambda-.0047*p+.0056439*w-a;
%F(9)=.0523244+2.903846*v-.6319993/c-.0003297/eta/eta+.0000121/eta/eta/eta-u
F(10)=(beta*100*a*(p-eta*p+eta*z-z))/(beta*100*a*eta*theta*p*(1-d)+p*(1+theta)*(1-beta*(1-d)*(1-lambda)))-c;
end
function [xx,exitflag] = DMP
beta=0.998418435031256; %monthly (yearly=0.981185442086183)
w=21.42592; %Period 4
p=28.1777866666667; %Period 4
z=0.0113878916464317; %Period 4
b=0.0119785793333333; %Period 3
d=0.0169109966666667; %Period 4
optimoptions('fsolve');
options = optimoptions('fsolve');
%options.Algorithm = 'trust-region'
options.Algorithm = 'Levenberg-Marquardt';
options.MaxFunctionEvaluations = 1000000;
options.MaxIterations = 100000;
options.StepTolerance = 1e-20;
options.FunctionTolerance = 1e-5;
options.OptimalityTolerance: 1e-5;
st = 0.01;
ed = 0.04;
u_ini = (ed-st).*rand(1,1) + st;
st = 0.005;
ed = 0.017;
v_ini = (ed-st).*rand(1,1) + st;
theta_ini=v_ini/u_ini;
st = 0.02;
ed = 0.06;
a_ini = (ed-st).*rand(1,1) + st;
st = 0.06;
ed = 0.20;
eta_ini = (ed-st).*rand(1,1) + st;
st = 0.01;
ed = 0.03;
lambda_ini = (ed-st).*rand(1,1) + st;
st = 12;
ed = 30;
c_ini = (ed-st).*rand(1,1) + st;
x0=[u_ini,v_ini,theta_ini,eta_ini,a_ini,lambda_ini,c_ini];
[x fval exitflag]=fsolve(@equations,x0,options,beta,w,p,z,b,d);
u=x(1);
v=x(2);
theta=x(3);
eta=x(4);
a=x(5);
lambda=x(6);
c=x(7);
xx=[a,eta,lambda,c,u,v];
end
It ran without error, and produced (in one run) —
xxx =
0.0399 0.0820 0.0167 18.9749 0.0229 0.0101
0 0 0 0 0 0
0.0413 0.1399 0.0165 24.0175 0.0418 0.0078
0.0399 0.0820 0.0167 18.9749 0.0229 0.0101
0.0399 0.0820 0.0167 18.9749 0.0229 0.0101
0.0399 0.0820 0.0167 18.9749 0.0229 0.0101
0.0399 0.0820 0.0167 18.9749 0.0229 0.0101
0.0399 0.0820 0.0167 18.9749 0.0229 0.0101
The zeros indicate a run in which the solution was not found. The random values are the reason that it will not always converge on a solution. This varies between runs.
.
Oh.... It works. I am so thankful to you. I learned a lot.
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
