Programme not running when using fsolver.

I would be glad if anyone could help me identify the reason my code isn't running when using fsolve.

 Accepted Answer

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

I modified the code but i keep getting errors.
Do you suggest I use a different solver?
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.
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;]
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.
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
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:
Oh kk, I get it now. Thank you.Thank for the link.
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics 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!