이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Saving vector result from each loop if solution is correct.
조회 수: 2 (최근 30일)
이전 댓글 표시
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?
채택된 답변
Star Strider
2021년 10월 31일
댓글 수: 14
Star Strider
2021년 10월 31일
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.
.
Star Strider
2021년 10월 31일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Deokjae Jeong
2021년 10월 31일
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
Star Strider
2021년 10월 31일
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/785178/image.png)
.
Deokjae Jeong
2021년 10월 31일
편집: Deokjae Jeong
2021년 10월 31일
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
Star Strider
2021년 10월 31일
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.
.
Deokjae Jeong
2021년 10월 31일
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.
Star Strider
2021년 10월 31일
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.
.
Star Strider
2021년 10월 31일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
추가 답변 (0개)
참고 항목
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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)