Problem with fsolve and matlabFunction

Hi
I have built a function that I want to solve with fsolve, but when I run it I get an error. I think i might call for the function in the wrong way or I can't use matlabFunction in the way i wrote it in the code. Here is the code to request the function and the error:
n=[1 3 5]
Vd=100;
Ma=50;
x0=[0;0;0]
f=fsolve(@(x1,x2,x3) HarmonicElimination2(n,Vd,Ma),[0;0;0])
Error using fsolve (line 256)
FSOLVE requires all values returned by user functions to be of data type double.
Error in test (line 8)
f=fsolve(@(x1,x2,x3) HarmonicElimination2(n,Vd,Ma),[0;0;0])
My function look like this
function [FF]=HarmonicElimination2(n,Vd,Ma)
% n - numbers of angles that needs to be solved
% Vd - Voltage source
% Ma - Controlling the fundamental component of voltage to MA magnitude (rms)
N = length(n); %how many angles we are suppose to solve for
x = sym('x',[N,1]); % Creating varable names for the angles.
F=[];
%%%%QUARTER WAVE SYMMETRY %%%%
for i=[1:N] %Buildning a matrix for fsolve
m=n(i);
%If we want to control the fundamental magnitude
if i==1
Ma=Ma;
else
Ma=0;
end
% Set up of the rows going to fsolv. MA is the disered magnitud of the
% fundamental magnitude if we want to solve for that oterwhise MA=0
f=(((4*Vd)/(m*pi))*(1+(2*(sum(cos(m*x(2:2:end))))-(sum(cos(m*x(1:2:end)))))))-Ma;
F=[F; f];
end
FF=matlabFunction(F)
end

6 Comments

Elin - I suspect the problem is that you are using symbolic variables in your HarmonicElimination2 function and that the output is a FF is of type sym. Can you verify that this is the case by just running the function with some dummy inputs and then observe the output? If this is true, then the error message makes sense since fsolve expects inputs of type double.
I'm using matlabFunction to get rid of the symbolic variables and FF is a function. When I'm using one variable (x1 -> n=[1]) I can run it with this code:
n=[1]
Vd=100;
Ma=50;
x0=[0]
F=HarmonicElimination2(n,Vd,Ma)
x = fsolve(F,x0)
But if I want two input variables (x1 and x2 -->n=[1 3]) I can't run it anymore
n=[1 3]
Vd=100;
Ma=50;
x0=[0; 0]
F=HarmonicElimination2(n,Vd,Ma)
x = fsolve(F,x0)
Elin - if matlabFunction returns a function handle, and so FF is a function handle, then HarmoncElemination2 returns a function handle. And so the output from
f=fsolve(@(x1,x2,x3) HarmonicElimination2(n,Vd,Ma),[0;0;0])
is a function handle and not the double that fsolve is expecting. If HarmonicElimination2 is returning a handle to a function with three input variables, then why doesn't the following (which you have seem to already have tried) not work?
FF = HarmonicElimination2(n,Vd,Ma);
f=fsolve(FF,[0;0;0]);
What errors are you observing when n is (for example) [1 3].
When I'm using
n=[1 3]
f=fsolve(@(x1,x2) HarmonicElimination2(n,Vd,Ma),[0;0])
I get the error:
Error using fsolve (line 256)
FSOLVE requires all values returned by user functions to be of data type double.
Error in test (line 5)
f=fsolve(@(x1,x2) HarmonicElimination2(n,Vd,Ma),[0;0])
When I'm using
n=[1 3]
FF = HarmonicElimination2(n,Vd,Ma);
f=fsolve(FF,[0;0]);
I get the error:
Error using
symengine>makeFhandle/@(x1,x2)[cos(x1).*(-1.273239544735163e2)+cos(x2).*2.546479089470325e2+7.732395447351627e1;cos(x1.*3.0).*(-4.244131815783876e1)+cos(x2.*3.0).*8.488263631567752e1+4.244131815783876e1]
Not enough input arguments.
Error in fsolve (line 217)
fuser = feval(funfcn{3},x,varargin{:});
Error in test (line 8)
f=fsolve(FF,[0;0]);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
When I'm running the last one with one variable (x1 --> n=[1]) it works
Please post what the variable FF is once you have assigned it via
FF = HarmonicElimination2(n,Vd,Ma);
Also, include its type by evaluating
class(FF)
FF =
@(x1,x2)[cos(x1).*(-1.273239544735163e2)+cos(x2).*2.546479089470325e2+7.732395447351627e1;cos(x1.*3.0).*(-4.244131815783876e1)+cos(x2.*3.0).*8.488263631567752e1+4.244131815783876e1]
Clas(FF) gives
ans =
function_handle

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 7 Mar 2015
Edited: Geoff Hayes on 7 Mar 2015
Elin - since your function handle FF is defined as
FF = ...
@(x1,x2)[cos(x1).*(-1.273239544735163e2)+cos(x2).*2.546479089470325e2+7.732395447351627e1;cos(x1.*3.0).*(-4.244131815783876e1)+cos(x2.*3.0).*8.488263631567752e1+4.244131815783876e1];
then it is a function that expect two inputs, x1 and x2. Now consider how you are using it with fsolve
f=fsolve(FF,[0;0]);
where there is only one input which is a vector of two elements. So fsolve will pass an array or vector of two elements into FF. This array is just one input parameter to FF whereas it is expecting two and so the error is clear.
You may have to wrap FF in another function as
modFF = @(x)FF(x(1),x(2));
where modFF is a handle to a function that takes one input which is assumed to be a vector of two elements. Try modifying your code to
n = [1 3]
FF = HarmonicElimination2(n,Vd,Ma);
modFF = @(x)FF(x(1),x(2));
f = fsolve(modFF,[0;0]);
and see if the above does what you expect.

2 Comments

Oh it is working now!!! Thank you :)
Glad it worked out, Elin!

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 7 Mar 2015

Commented:

on 7 Mar 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!