MATLAB: I keep getting this error, ” Failure in initial objective function evaluation. FSOLVE cannot continue.”

equations solvefsolve

Hi, currently I trying to solve for a system of nonlinear equations (5 equatios in total).
Here is my function part
% Coeffecient
M1 = 0.5;
M2 = 0.75;
M3 = 0.95;
M4 = 1.102;
M5 = 1.102;
K = 1;
Kc = 1;
Kk = 3;
KR = 0.95;
G = 0.001;
GR = 0.1;
Gc = 0.005;
B = 0.15;
A= 1;
w= 1:0.1:2;
%Set up equation
function F = MyFunction(x)
F(1 )= x(1).*(-M1.*(w.^2) + K + Kc +KR + G.*1i.*w) - (A.*(exp(1i.*w)) + Kc.*x(4) + KR.*x(3));
F(2) = x(2).*(-M2.*(w.^2) + K + Kc +KR + G.*1i.*w) - (A.*(exp(1i.*w)) + Kc.*x(5) + KR.*x(3));
F(3) = x(3).*(-M3.*(w.^2) + 2.*kk + GR.*1i.*w) - (KR.*(x(1) + (x(2))));
F(4) = x(4).*(-M4.*(w.^2) + Kc + kk + Gc.*1i.*w) - B.*1i.*(w.^3).*((x(4)).^3) - A.*(exp(1i.*w)) - Kc.*x(1);
F(5) = x(5).*(-M5.*(w.^2) + Kc + kk + Gc.*1i.*w) - B.*1i.*(w.^3).*((x(4)).^3) - A.*(exp(1i.*w)) - Kc.*x(2);
end
Then I create a different file to call back to it
x0 = [1 1 1 1 1];
x = fsolve(@MyFunction,x0);
So the idea is to solve x1, x2, x3, x4, x5 as a function of w. But I have tried fixing w to 1 value, as well as move the coeffecient to the 2nd file. But I still could fix this message that MATLAB gave me " Failure in initial objective function evaluation. FSOLVE cannot continue"
Anybody have a way for me to fix this

Best Answer

fsolve fails to continue because the function MyFunction doesnot not have access to the variables (M1, M2, M3...). You can use global variables for MyFunction to work without errors.
global param
param.M1 = 0.5;
param.M2 = 0.75;
param.M3 = 0.95;
param.M4 = 1.102;
param.M5 = 1.102;
param.K = 1;
param.Kc = 1;
param.Kk = 3;
param.KR = 0.95;
param.G = 0.001;
param.GR = 0.1;
param.Gc = 0.005;
param.B = 0.15;
param.A= 1;
param.w= 1;
x0 = [1 1 1 1 1];
fun=@MyFunction;
x = fsolve(fun,x0);
function F = MyFunction(x)
global param
F(1 )= x(1).*(-param.M1.*(param.w.^2) + param.K + param.Kc +param.KR + param.G.*1i.*param.w) - (param.A.*(exp(1i.*param.w)) + param.Kc.*x(4) + param.KR.*x(3));
F(2) = x(2).*(-param.M2.*(param.w.^2) + param.K + param.Kc +param.KR + param.G.*1i.*param.w) - (param.A.*(exp(1i.*param.w)) + param.Kc.*x(5) + param.KR.*x(3));
F(3) = x(3).*(-param.M3.*(param.w.^2) + 2.*param.Kk + param.GR.*1i.*param.w) - (param.KR.*(x(1) + (x(2))));
F(4) = x(4).*(-param.M4.*(param.w.^2) + param.Kc + param.Kk + param.Gc.*1i.*param.w) - param.B.*1i.*(param.w.^3).*((x(4)).^3) -param.A.*(exp(1i.*param.w)) - param.Kc.*x(1);
F(5) = x(5).*(-param.M5.*(param.w.^2) + param.Kc + param.Kk + param.Gc.*1i.*param.w) - param.B.*1i.*(param.w.^3).*((x(4)).^3) - param.A.*(exp(1i.*param.w)) - param.Kc.*x(2);
end