matlab Attempted to access x(2); index out of bounds because
来源:学生作业帮 编辑:搜搜考试网作业帮 分类:英语作业 时间:2024/08/13 23:59:28
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;
在求解非线性方程时遇到问题
自定义方程
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](/uploads/image/z/4725602-26-2.jpg?t=matlab+Attempted+to+access+x%282%29%3B+index+out+of+bounds+because)
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
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
matlab Attempted to access x(2); index out of bounds because
Matlab新手,Attempted to access X(2); index out of bounds becau
matlab中出现 Attempted to access X(1); index out of bounds beca
Attempted to access a(:,2); index out of bounds because size
在MATLAB中,提示Attempted to access th(2,1); index out of bounds
matlab问题 Attempted to access w(1001); index out of bounds be
不知道为什么报这样的错:Attempted to access e(10,:); index out of bounds
运行Matlab时出现错误?Attempted to access rxd(500); index out of bou
Matlab 报错为Attempted to access f(64.4); index must be a posit
Matlab问题求大神帮忙Attempted to access Xs(-17.23); index must be a
matlab运行出现问题,Attempted to access e(1,0); index must be a pos
MATLAB运行时:Attempted to access (1,0); index must be a positiv