我很難在Matlab中使用fsolve函數來求解一組5個方程。在Matlab中求解5個非線性方程的系統
這裏是5個方程:
Y = A + d + E
Y + X = C + d + 2e中
2X = 4A + 2B + 2C
K1 =(d * b^3 /(A * C))*((P /縣)/(A + b + C + d + E))^ 2
K2 = b E /(dç )
y,x,k1,k2,P,Pref是我設置的所有參數,但希望將它們留在函數中,以便我可以在代碼中快速更改它們以找到新答案。 a,b,c,d,e是我想要解決的變量(它們是反應平衡方程式的組成)
我試圖硬編碼函數中的參數,但沒有工作。我只是不知道該怎麼做。我改變的每件事都會產生一個新的錯誤。最常見的是數據類型必須是「雙」。
編輯:添加代碼
第一功能:
function F = myfun(Q,I)
a = Q(1);
b = Q(2);
c = Q(3);
d = Q(4);
e = Q(5);
x = I(1);
y = I(2);
k1 = I(3);
k2 = I(4);
P = I(5);
Pref = I(6);
F(1) = a + d + e - y;
F(2) = c + d + 2*e - y - x;
F(3) = 4*a + 2*b + 2*c - 2*x;
F(4) = ((d * b^3)/(a*c))*((P/Pref)/(a+b+c+d+e))^2 - k1;
F(5) = (b*e)/(c*d);
下一個是程序:
%Q = [a,b,c,d,e]
%I = [x,y,k1,k2,P,Pref]
%The values for the inputs will be changed to vary the output
%Inputs:
x=5;
y=1;
k1=5;
Pref=1;
P=1;
k2=-0.01;
syms K
k1 = solve(log10(k1) - k1);
syms L
k2 = solve(log10(k2) - k2);
x = double(x);
y = double(y);
Pref = double(Pref);
P = double(P);
k1 = double(k1);
k2 = double(k2);
%Solving:
I = [x,y,k1,k2,P,Pref];
q = [0,0,0,0,0]; %initial guess
Q = fsolve(@myfun,[q,I])
當我運行這一點,這些錯誤出現:
Error using myfun (line 7)
Not enough input arguments.
Error in fsolve (line 218)
fuser = feval(funfcn{3},x,varargin{:});
Error in Coal (line 27)
Q = fsolve(@myfun,[q,I])
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
編輯2:陳GED的fsolve線,但仍然得到了錯誤:
Error using trustnleqn (line 28)
Objective function is returning undefined values at initial point. FSOLVE cannot continue.
Error in fsolve (line 376)
[x,FVAL,JACOB,EXITFLAG,OUTPUT,msgData]=...
Error in Coal (line 27)
fsolve(@(q) myfun(q,I),q)
EDIT3:改變了一些參數和初始猜測,我現在得到一個答案,但它也談到了這一點:
Solver stopped prematurely.
fsolve stopped because it exceeded the function evaluation limit,
options.MaxFunEvals = 500 (the default value).
ans =
0.0000 2.2174 3.7473 1.4401 3.8845
我怎樣才能讓它不會提前停止?
你可以顯示你寫的代碼嗎? – David
@David,我添加了代碼。 – RettTriplett
'fsolve'僅適用於功能的單輸入,即使它是一個矢量,所以嘗試'fsolve(@(Q)myfun(Q,I),Q)'代替。 – David