我正在尝试解决一个非常大的耦合非线性方程组。在此thread以及Matalb(第一个示例)的相关帮助之后,我尝试编写以下代码:
%% FSOLVE TEST #2
clc; clear; close all
%%
global a0 a1 a2 a3 a4 h0 TM JA JB
a0 = 2.0377638272727268;
a1 = -7.105521894545453;
a2 = 9.234000147272726;
a3 = -5.302489919999999;
a4 = 1.1362478399999998;
h0 = 45.5;
TM = 0.00592256;
JA = 1.0253896074561006;
JB = 1.3079437258774012;
%%
global N
N = 5;
XA = 0;
XB = 15;
dX = (XB-XA)/(N-1);
XX = XA:dX:XB;
y0 = JA:(JB-JA)/(N-1):JB;
plot(XX,y0,'o')
[x,fval] = fsolve(@nlsys,y0);
函数 nlsys 如下:
function S = nlsys(x)
global a1 a2 a3 a4 N TM h0 dX JA JB
H = h0^2/12;
e = cell(N,1);
for i = 2:N-1
D1 = (x(i+1) - x(i-1))./2./dX;
D2 = (x(i+1) + x(i-1) - 2.*x(i))./(dX^2);
f = a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3;
g = - H.* (a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3)./(x(i).^5);
b = (H/2) .* (5*a1 + 8*a2.*x(i) + 9*a3.*x(i).^2 + 8*a4.*x(i).^3)./(x(i).^6);
e{i} = @(x) f + b.*(D1.^2) + g.*D2 - TM;
end
e{1} = @(x) x(1) - JA;
e{N} = @(x) x(N) - JB;
S = @(x) cellfun(@(E) E(x), e);
当我运行程序时,Matlab会出现以下错误:
使用fsolve时出错(第280行) FSOLVE要求用户函数返回的所有值都是数据类型为double。
fsolve_test2出错(第32行) [x,fval] = fsolve(@ nlsys,y0);
我的错误在哪里? 提前谢谢。
Petrus的