Failure in initial user-supplied objective function evaluation

I am trying to find the roots of a system of two equations with two variables and want to find the solution for different values of one of the parameters that is 'Dr'. As a start I created an .m file for the functions as
function F = root(x)
%parameters
global mh mv dha dhc qa pa qc pc bvh bhav bhcv na zeta sh sv Dr
%equations
F = [(x(2)*x(1)+mh*qa*pa-(mh+dha+na+zeta))*(X(2)*x(1)-(mh+dhc))*(Dr*((x(1)).^2)+x(2)*x(1)-mv)-mh*qc*pc*zeta*(Dr*((x(1)).^2)+x(2)*x(1)-mv)+bvh*(sv/sh)*(bhcv*zeta-bhav*(x(2)*x(1)-(mh+dhc)));
x(2).*(x(2).*x(1)-1.2).*(0.2*(x(1).^2)+x(2).*x(1)-1.2)+(x(2).*x(1)-1.993).*x(2).*(0.2*(x(1).^2)+x(2).*x(1)-1.2)+(x(2).*x(1)-1.993).*(x(2).*x(1)-1.2).*(.4*x(1)+x(2))-0.00336*x(1)-1.251691869*x(2)];
end
Then I recalled it in a separate file with this codes
clear
close all
global mh mv dha dhc qa pa qc pc bvh bhav bhcv na zeta sh sv Dr;
%parameters
mh=.7; mv=1.2; dha=0.2; dhc=0.5;
qa=0.1; pa=0.1;
qc=0.3; pc=0.1; na=0.7; zeta=.4;
bvh= 3; bhav=.2; bhcv=2.6 sh=426.87;
sv=884.57;
Dr=0.2;
x0 = [-10 -10];
[x,fval] = fsolve(@root,x0);
I don't know what is the problem! I tried different initial values for x0 but keep getting the same error.

 Risposta accettata

Stephan
Stephan il 26 Ago 2018
Modificato: Stephan il 26 Ago 2018
Hi,
function myfun
%parameters
mh=.7;
mv=1.2;
dha=0.2;
dhc=0.5;
qa=0.1;
pa=0.1;
qc=0.3;
pc=0.1;
na=0.7;
zeta=.4;
bvh= 3;
bhav=.2;
bhcv=2.6;
sh=426.87;
sv=884.57;
Dr=0.2;
x0 = [-2.25 0.5];
[x,fval] = fsolve(@root,x0)
function F = root(x)
%equations
F = [(x(2)*x(1)+mh*qa*pa-(mh+dha+na+zeta))*(x(2)*x(1)-(mh+dhc))*(Dr*((x(1)).^2)+x(2)*x(1)-mv)-mh*qc*pc*zeta*(Dr*((x(1)).^2)+x(2)*x(1)-mv)+bvh*(sv/sh)*(bhcv*zeta-bhav*(x(2)*x(1)-(mh+dhc)));...
x(2).*(x(2).*x(1)-1.2).*(0.2*(x(1).^2)+x(2).*x(1)-1.2)+(x(2).*x(1)-1.993).*x(2).*(0.2*(x(1).^2)+x(2).*x(1)-1.2)+(x(2).*x(1)-1.993).*(x(2).*x(1)-1.2).*(.4*x(1)+x(2))-0.00336*x(1)-1.251691869*x(2)];
end
end
Save this script with the same name of the outer function (here myfun).
In the equation of your objective function there was one X(2) instead of x(2). Since i like it better to use a nested function here, i killed your global variables...
Next Time you get an error message please provide the whole red text. Makes helping easier.
Also note the x0 values i have choosen and compare the results to your x0.
Best regards
Stephan

Più risposte (0)

Categorie

Scopri di più su General Applications in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by