functions retuning functions, fsolve
7 views (last 30 days)
Show older comments
Hallo Matlab friends,
I need some help with functions returning functions. I would like to solve nonlinear equations with fsolve for x. My Problem is, how can i put functions(x) into a function which returns functions(x) that i can use in fsolve?
Here is my Problem:
y1 = @(x) 2.*x;
y2 = @(x) x.^2;
[z1,z2] = @(x) fun(y1,y2);
F = @(x) z1(x) + z2(x)
x0 = [0 0 0]';
solu = fsolve(F,x0);
The function fun looks like
function [z1,z2] = fun(y1,y2)
z1 = 4.*y1.^2;
z2 = y2.^3;
end
If i run my code i get the error message:
Only functions can return multiple values.
How can i solve such a problem? I really need some help.
2 Comments
Answers (4)
Walter Roberson
on 2 Aug 2016
function z = F
syms x
[z1, z2] = fun(x,x)
z = matlabFunction(z1 + z2);
7 Comments
Stefan Holzinger
on 3 Aug 2016
Edited: Stefan Holzinger
on 3 Aug 2016
1 Comment
Walter Roberson
on 3 Aug 2016
Your M is 4 x 4, your z1 and z2 are 1 x 4. You have M*x + z1 + z2 . MATLAB always treats (resolved) symbolic names as scalars, so M*x is 4 x 4, and you cannot add 1 x 4 to that. It is not possible in MATLAB to say "in the following, assume that the symbolic name x is a matrix of appropriate size to make all of the operations work out". And even if it did, because M is 4 by 4, no matter what size x was, M*x would have to have a leading dimension of 4 and that is not going to be something you can add to the row vectors z1 and z2.
If you need x to be a matrix in F then you need to use something like
x = sym('x', 4, 1);
h1 = 2*c1.' + 4*x;
h2 = c2.' + x;
You will probably also want to http://www.mathworks.com/help/symbolic/matlabfunction.html#zmw57dd0e96800 and pay attention to the way to have a list of variables be incorporated into an input vector instead of having to appear as individual arguments.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!