matlab Attempted to access x(2); index out of bounds because numel(x)=1.在求解非线性方程时遇到问题自定义方程 function y = myfun(x) %定义函y=[3*x(1) - cos(x(2)* x(3))- 0.5;x(1)^2 - 81*(x(2) +0.1)^2 + sin(x(3) ) + 1.06;eps(-x (1) *

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/30 10:16:26
matlab Attempted to access x(2); index out of bounds because numel(x)=1.在求解非线性方程时遇到问题自定义方程 function y = myfun(x) %定义函y=[3*x(1) - cos(x(2)* x(3))- 0.5;x(1)^2 - 81*(x(2) +0.1)^2 + sin(x(3) ) + 1.06;eps(-x (1) *

matlab Attempted to access x(2); index out of bounds because numel(x)=1.在求解非线性方程时遇到问题自定义方程 function y = myfun(x) %定义函y=[3*x(1) - cos(x(2)* x(3))- 0.5;x(1)^2 - 81*(x(2) +0.1)^2 + sin(x(3) ) + 1.06;eps(-x (1) *
matlab Attempted to access x(2); index out of bounds because numel(x)=1.
在求解非线性方程时遇到问题
自定义方程
function y = myfun(x) %定义函
y=[3*x(1) - cos(x(2)* x(3))- 0.5;x(1)^2 - 81*(x(2) +0.1)^2 + sin(x(3) ) + 1.06;eps(-x (1) * x(2))+ 20 * x(3)+(10*pi-3)/3 ]; %非线性方程组中对应的f1 ,f2
运行时出现
Error in ==> myfun at 2
Attempted to access x(2); index out of bounds because numel(x)=1.
PS:droyden code
function [xv,it]=broyden(x,f,n,tol)
% Broyden's method for solving a system of n non-linear equations
% in n variables.
%
% Example call:[xv,it]=broyden(x,f,n,tol)
% Requires an initial approximation column vector x.tol is required
% accuracy.User must define function f,for example see page 115.
% xv is the solution vector,parameter it is number of iterations
% taken.WARNING.Method may fail,for example,if initial estimates
% are poor.
%
fr=zeros(n,1); it=0; xv=x;
%Set initial Br
Br=eye(n);
fr=feval(f,xv);
while norm(fr)>tol
it=it+1;
pr=-Br*fr;
tau=1;
xv1=xv+tau*pr; xv=xv1;
oldfr=fr; fr=feval(f,xv);
%Update approximation to Jacobian using Broyden?s formula
y=fr-oldfr; oldBr=Br;
oyp=oldBr*y-pr; pB=pr'*oldBr;
for i=1:n
for j=1:n
M(i,j)=oyp(i)*pB(j);
end;
end;
Br=oldBr-M./(pr'*oldBr*y);
end;

matlab Attempted to access x(2); index out of bounds because numel(x)=1.在求解非线性方程时遇到问题自定义方程 function y = myfun(x) %定义函y=[3*x(1) - cos(x(2)* x(3))- 0.5;x(1)^2 - 81*(x(2) +0.1)^2 + sin(x(3) ) + 1.06;eps(-x (1) *
x0=[2.5 3.6 3.5]';%%初值
n=3;%%几个方程组
tol=1e-6;%%精度要求
[xv,it]=broyden(x0,'myfun',3,tol)%%x0是初值,你的myfun里有三个x,说明x是一维矩阵,另外根据broyden函数体判断得知,x必须是列向量
运行结果:
xv =
0.4985
-0.2026
-0.4736
it =
191