You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Programme not running when using fsolver.
2 views (last 30 days)
Show older comments
I would be glad if anyone could help me identify the reason my code isn't running when using fsolve.
4 Comments
Honey Adams
on 7 Aug 2018
I posted it there since the conversation here got stuck.
Honey Adams
on 7 Aug 2018
Doy you mind helping me out ?Thank you
Accepted Answer
Walter Roberson
on 6 Aug 2018
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 Comments
Honey Adams
on 6 Aug 2018
I modified the code but i keep getting errors.
Honey Adams
on 6 Aug 2018
Do you suggest I use a different solver?
Honey Adams
on 6 Aug 2018
Edited: Walter Roberson
on 6 Aug 2018
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
on 6 Aug 2018
I have attached the Matlab in the up there.I would be grateful if you could check it out for me.
Walter Roberson
on 6 Aug 2018
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
on 6 Aug 2018
Edited: Honey Adams
on 6 Aug 2018
w' multiplies all the centre term, left term and right term.I have modified it again below.
Honey Adams
on 6 Aug 2018
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
on 6 Aug 2018
Edited: Walter Roberson
on 7 Aug 2018
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
on 7 Aug 2018
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)
Honey Adams
on 7 Aug 2018
The code attached bears the data for the functions.
Walter Roberson
on 7 Aug 2018
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
on 7 Aug 2018
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..
Honey Adams
on 7 Aug 2018
Thus when trying to test the function. with the initial xo values.
Walter Roberson
on 7 Aug 2018
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
on 7 Aug 2018
KK thank you .i changed the directory as well. Thank you so much
Honey Adams
on 7 Aug 2018
It works now after i changed the directory .
Honey Adams
on 7 Aug 2018
Solver stopped prematurely.
fsolve stopped because it exceeded the function evaluation limit, options.MaxFunEvals = 2200 (the default value)
Honey Adams
on 7 Aug 2018
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
on 7 Aug 2018
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
on 7 Aug 2018
Edited: Stephen23
on 7 Aug 2018
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
on 7 Aug 2018
Edited: Stephen23
on 7 Aug 2018
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
on 7 Aug 2018
Oh kk, I get it now. Thank you.Thank for the link.
Honey Adams
on 7 Aug 2018
Edited: Honey Adams
on 7 Aug 2018
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
on 7 Aug 2018
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
on 7 Aug 2018
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.
Honey Adams
on 8 Aug 2018
oh kk. Thank you very much, Walter and Alan.
More Answers (0)
See Also
Categories
Find more on Historical Contests in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)