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 件のコメント

Geoff Hayes
Geoff Hayes 2015 年 3 月 7 日
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.
Elin
Elin 2015 年 3 月 7 日
編集済み: Elin 2015 年 3 月 7 日
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)
Geoff Hayes
Geoff Hayes 2015 年 3 月 7 日
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].
Elin
Elin 2015 年 3 月 7 日
編集済み: Elin 2015 年 3 月 7 日
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
Geoff Hayes
Geoff Hayes 2015 年 3 月 7 日
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 2015 年 3 月 7 日
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

サインインしてコメントする。

回答 (1 件)

Geoff Hayes
Geoff Hayes 2015 年 3 月 7 日
編集済み: Geoff Hayes 2015 年 3 月 7 日

1 投票

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 件のコメント

Elin
Elin 2015 年 3 月 7 日
Oh it is working now!!! Thank you :)
Geoff Hayes
Geoff Hayes 2015 年 3 月 7 日
Glad it worked out, Elin!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Mathematics についてさらに検索

質問済み:

2015 年 3 月 7 日

コメント済み:

2015 年 3 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by