Problem with fsolve and matlabFunction

9 views (last 30 days)
Elin
Elin on 7 Mar 2015
Commented: Geoff Hayes on 7 Mar 2015
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
Geoff Hayes
Geoff Hayes on 7 Mar 2015
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)
Elin
Elin on 7 Mar 2015
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.

Community Treasure Hunt

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

Start Hunting!