現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)