Programme not running when using fsolver.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I would be glad if anyone could help me identify the reason my code isn't running when using fsolve.
댓글 수: 4
I posted it there since the conversation here got stuck.
Doy you mind helping me out ?Thank you
채택된 답변
Walter Roberson
2018년 8월 6일
Your V and Z is 10 x 6 and your r and s is 6 x 1 and your k is 10 x 1. These all go together in a way such that for
f(1)=(1/(1+b^2 +g^2)*((V'*V)^-1 * V'*((center-(W*r + Z*s))+ (left-(W*r +Z*s)*...
b - k*d)*b + (right-(W*r+Z*s)*g - k*h)*g)))-a;
the right hand side is 6 x 1. A 6 x 1 vector cannot be stored in a single numeric location such as f(1)
The f(2) and f(3) entries have the same size difficulty.
You have
f(4)= ((a'*V' + r' * W' + s'*Z')*(V*a + W*r + Z*s)^-1 *(left'*(V*a +W*r + Z*s)...
-(a'*V' + r'*W' +s'*Z')*k*d))- b;
The ^-1 has priority over matrix multiplication, so the (V*a + W*r + Z*s) is computed and attempted to be raised to -1. But (V*a + W*r + Z*s) is 10 x 1 and you can only raise a non-scalar with ^-1 if you are working with a square matrix.
f(5) has the same problems as f(4).
f(6) and f(7) are okay: they each compute scalars.
Your ^-1 of matrices are matrix inverse requests, just as if you had coded inv() . However, you should avoid coding ^-1 or inv() calls as that operation is not numerically stable. You should be changing to using the / or \ operators. For example,
(V'*V)^-1 * V'*ETC
should be re-coded as
(V'*V) \ V'
댓글 수: 26
I modified the code but i keep getting errors.
Do you suggest I use a different solver?
Honey Adams
2018년 8월 6일
편집: Walter Roberson
2018년 8월 6일
f=[(1/(1+b^2 + g^2))*((V'*V)\ (V'*(center- W*r + Z*s)+ (left-(W*r +Z*s)*b...
- k*d)*b + (right-(W*r + Z*s)*g - k*h)*g)) -a;
(1/(1+b^2 +g^2))*((W'*W)\(W'*(center- V*a + Z*s)+ (left-(V*a +Z*s)*...
b - k*d)*b + (right-(V*a + Z*s)*g - k*h)*g))-r;
(1/(1+b^2 +g^2))*((Z'*Z)\(Z'*(center- V*a + W*r)+ (left-(V*a +W*r)*...
b - k*d)*b + (right-(V*a + W*r)*g - k*h)*g))-s;
((a'*V' + r' * W' + s'*Z')*(V*a + W*r + Z*s))\(left'*(V*a +W*r + Z*s)...
-(a'*V' + r'*W' +s'*Z')*k*d)- b;
((a'*V' + r' * W' + s'*Z')*(V*a + W*r + Z*s))\(right'*(V*a +W*r + Z*s)...
-(a'*V' + r'*W' +s'*Z')*k*h)- g;
(1/10*(left'*k -(a'*V' + r'*W' + s'*Z')*k*b))-d;
(1/10*(left'*k -(a'*V' + r'*W' + s'*Z')*k*g))- h;]
I have attached the Matlab in the up there.I would be grateful if you could check it out for me.
In your second group of your revised f, the part
W'*(center- V*a + Z*s)
is 6 x 1, but (left-(V*a +Z*s)*b - k*d)*b is 10 x 1, so it is not possible to add the two.
Honey Adams
2018년 8월 6일
편집: Honey Adams
2018년 8월 6일
w' multiplies all the centre term, left term and right term.I have modified it again below.
f=[(1/(1+b^2 + g^2))*(((V'*V)\ V')*((center- W*r + Z*s)+ (left-(W*r +Z*s)*b... - k*d)*b + (right-(W*r + Z*s)*g - k*h)*g)) -a;
(1/(1+b^2 +g^2))*(((W'*W)\ W')*((center- V*a + Z*s)+ (left-(V*a +Z*s)*...
b - k*d)*b + (right-(V*a + Z*s)*g - k*h)*g))-r;
(1/(1+b^2 +g^2))*(((Z'*Z)\Z')*((center- V*a + W*r)+ (left-(V*a +W*r)*...
b - k*d)*b + (right-(V*a + W*r)*g - k*h)*g))-s;
((a'*V' + r' * W' + s'*Z')*(V*a + W*r + Z*s))\(left'*(V*a +W*r + Z*s)...
-(a'*V' + r'*W' +s'*Z')*k*d)- b;
((a'*V' + r' * W' + s'*Z')*(V*a + W*r + Z*s))\(right'*(V*a +W*r + Z*s)...
-(a'*V' + r'*W' +s'*Z')*k*h)- g;
(1/10*(left'*k -(a'*V' + r'*W' + s'*Z')*k*b))-d;
(1/10*(left'*k -(a'*V' + r'*W' + s'*Z')*k*g))- h;]
Honey Adams
2018년 8월 6일
편집: Walter Roberson
2018년 8월 7일
Hello Walter, please I tried working on it but I will need your help as it still isn't running.
function f = FIFON(x,V,W,Z,k,center,left,right)
a=[x(1);x(2);x(3);x(4);x(5);x(6)];
r=[x(7);x(8);x(9);x(10);x(11);x(12)];
s=[x(13);x(14);x(15);x(16);x(17);x(18)];
b=x(19);
g=x(20);
d=x(21);
h=x(22);
f=[(1/(1+b^2 + g^2))*(((V'*V)\ V')*((center- W*r + Z*s)+ (left-(W*r +Z*s)*b...
- k*d)*b + (right-(W*r + Z*s)*g - k*h)*g)) -a;
(1/(1+b^2 +g^2))*(((W'*W)\ W')*((center- V*a + Z*s)+ (left-(V*a +Z*s)*...
b - k*d)*b + (right-(V*a + Z*s)*g - k*h)*g))-r;
(1/(1+b^2 +g^2))*(((Z'*Z)\Z')*((center- V*a + W*r)+ (left-(V*a +W*r)*...
b - k*d)*b + (right-(V*a + W*r)*g - k*h)*g))-s;
((a'*V' + r' * W' + s'*Z')*(V*a + W*r + Z*s))\(left'*(V*a +W*r + Z*s)...
-(a'*V' + r'*W' +s'*Z')*k*d)- b;
((a'*V' + r' * W' + s'*Z')*(V*a + W*r + Z*s))\(right'*(V*a +W*r + Z*s)...
-(a'*V' + r'*W' +s'*Z')*k*h)- g;
(1/10*(left'*k -(a'*V' + r'*W' + s'*Z')*k*b))-d;
(1/10*(right'*k -(a'*V' + r'*W' + s'*Z')*k*g))- h]
V;W;Z;k;center;left; right; fun=@(x)FIFON(x,V,W,Z,k,center,left,right)
xo = [0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.5 0.6 0.2 0.2 0.2 0.2 0.2]; X = fsolve(fun,xo)
The code attached bears the data for the functions.
function f = FIFON(x,V,W,Z,k,center,left,right)
a=[x(1);x(2);x(3);x(4);x(5);x(6)];
r=[x(7);x(8);x(9);x(10);x(11);x(12)];
s=[x(13);x(14);x(15);x(16);x(17);x(18)];
b=x(19);
g=x(20);
d=x(21);
h=x(22);
f=[(1/(1+b^2 + g^2))*(((V'*V)\ V')*((center- W*r + Z*s)+ (left-(W*r +Z*s)*b...
- k*d)*b + (right-(W*r + Z*s)*g - k*h)*g)) - a;
(1/(1+b^2 +g^2))*(((W'*W)\ W')*((center- V*a + Z*s)+ (left-(V*a +Z*s)*...
b - k*d)*b + (right-(V*a + Z*s)*g - k*h)*g)) - r;
(1/(1+b^2 +g^2))*(((Z'*Z)\Z')*((center- V*a + W*r)+ (left-(V*a +W*r)*...
b - k*d)*b + (right-(V*a + W*r)*g - k*h)*g)) - s;
((a'*V' + r' * W' + s'*Z')*(V*a + W*r + Z*s))\(left'*(V*a +W*r + Z*s)...
-(a'*V' + r'*W' +s'*Z')*k*d) - b;
((a'*V' + r' * W' + s'*Z')*(V*a + W*r + Z*s))\(right'*(V*a +W*r + Z*s)...
-(a'*V' + r'*W' +s'*Z')*k*h) - g;
(1/10*(left'*k -(a'*V' + r'*W' + s'*Z')*k*b)) - d;
(1/10*(right'*k -(a'*V' + r'*W' + s'*Z')*k*g)) - h];
Spacing matters.
This is the error message i got. Error using vertcat Dimensions of matrices being concatenated are not consistent.
Error in FIFON (line 11) f=[(1/(1+b^2 + g^2))*(((V'*V)\ V')*((center- W*r + Z*s)+ (left-(W*r +Z*s)*b..
Thus when trying to test the function. with the initial xo values.
Did you copy in my new code? It may look the same as your old code, but notice that before in the first part of f you ended the clause with -a and my modified code ends with - a with a space between the - and the a . This matters for MATLAB.
KK thank you .i changed the directory as well. Thank you so much
It works now after i changed the directory .
Solver stopped prematurely.
fsolve stopped because it exceeded the function evaluation limit, options.MaxFunEvals = 2200 (the default value)
please cnfirm my function handle. fun=@(x)FIFON1(x,V,W,Z,k,center,left,right) In matlab documentation, the function handle is passed without the parameters like this fun=@FIFON1 .My function handle works when defined as fun=@(x)FIFON1(x,V,W,Z,k,centre,left,right) but does work when not defined as fun=@FIFON1 as explained https://www.mathworks.com/help/optim/ug/fsolve.html. Why does this happen?
V;W;Z;k;center;left; right;
fun=@(x)FIFON(x,V,W,Z,k,center,left,right);
opt = optimoptions('fsolve', 'Display', 'final', 'MaxFunctionEvaluations', 100000, 'MaxIterations', 5000);
xo = [0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.5 0.6 0.2 0.2 0.2 0.2 0.2];
x = fsolve(fun, xo, opt);
display(x)
You need to pass extra parameters to the function; using an anonymous function is a good way of doing that.
Honey Adams
2018년 8월 7일
편집: Stephen23
2018년 8월 7일
It works perfectly fine now thank you, Walter. I just wanted to clarify why some function handles are defined as eg.
fun=@FIFI
and not
fun=@(x)FIFO(x,V,W,Z,k,center,left,right)
I couldn't find an explanation in the Matlab documentation other than their use in examples .
Both syntaxes create a function handle. The first syntax creates a handle to an existing named function. The second syntax creates a handle to an anonymous function (which in your example happens to call an existing function). Read more here:
Oh kk, I get it now. Thank you.Thank for the link.
Honey Adams
2018년 8월 7일
편집: Honey Adams
2018년 8월 7일
Equation solved, inaccuracy possible.
The vector of function values is near zero, as measured by the default value of the function tolerance. However, the last step was ineffective.
Are the results with information acceptable? I used your parameter settings for the optimoptions. I played around with the different algorithms and modifying the initial x0 values and it gives positive results sometimes. I will keep tweaking and select the best results.
In my opinion, it is not worthwhile tweaking the solution process. The equation was solved. The "last step was ineffective," but so what? This just means that your equation is not so smooth. But fsolve got a solution anyway.
Alan Weiss
MATLAB mathematical toolbox documentation
My tests suggest that there might be a solution near
[3.0764454923181308, -0.29750955941213153, 1.32522678326379184, -5.67281188388481006, 3.71962594859759532, 1.79157413120165088, 0.0913355342239042522, -3.3450226246250705, 2.5660435058715354, 0.0354122586159170138, -0.177027435264798, -1.15044309568988101, 1.65430815977474821, -1.10152163665358227, 0.626930903877788825, -0.316658524647466799, -0.740546143279594782, 0.803505679158440511, -44.5712374138825425, 2.09613318816875438, 149.797294636857373, -6.26552007745399386]
Because of the 22 dimensional nature of the problem, searching is expensive.
oh kk. Thank you very much, Walter and Alan.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
태그
참고 항목
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)
