이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Programme not running when using fsolver.
조회 수: 2 (최근 30일)
이전 댓글 표시
I would be glad if anyone could help me identify the reason my code isn't running when using fsolve.
댓글 수: 4
채택된 답변
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
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;]
Honey Adams
2018년 8월 6일
I have attached the Matlab in the up there.I would be grateful if you could check it out for me.
Walter Roberson
2018년 8월 6일
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.
Honey Adams
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;]
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]
Honey Adams
2018년 8월 7일
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)
Walter Roberson
2018년 8월 7일
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.
Honey Adams
2018년 8월 7일
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..
Walter Roberson
2018년 8월 7일
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.
Honey Adams
2018년 8월 7일
Solver stopped prematurely.
fsolve stopped because it exceeded the function evaluation limit, options.MaxFunEvals = 2200 (the default value)
Honey Adams
2018년 8월 7일
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?
Walter Roberson
2018년 8월 7일
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 .
Stephen23
2018년 8월 7일
편집: Stephen23
2018년 8월 7일
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:
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.
Alan Weiss
2018년 8월 7일
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
Walter Roberson
2018년 8월 7일
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.
추가 답변 (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 (한국어)