怎么在MATLAB中求雅克比矩阵?
MATLAB中jacobian是用来计算Jacobi矩阵的函数。
matlab解方程组的雅克比矩阵 求解线性方程组的雅克比迭代
matlab解方程组的雅克比矩阵 求解线性方程组的雅克比迭代
syms r l f
x=rcos(l)cos(f);
y=rcos(l)sin(f);
z=rsin(l);
J=jacobian([x;y;z],[r l f])
matlab 雅可比(Jacobi)迭代法 的问题 求解释
function x=j(e)
% 运用Jacobi迭代求解H(n)x=b,其中H(n)为n阶Hibert矩阵,b=h(n)x,其中x=(1,...,1)'
% n表示n阶Hibret矩阵,e表示要求的误
% 计算结果中,x表示方程组的解,m表示所用迭代的步数
h=[4 -1 1;4 -8 1;-2 1 5];%系数矩阵
x0=ones(3,1);%赋1
x=zeros(3,1);%赋0
y=x;%赋0
b=[7;-21;15];%列矢量
s=norm(x-x0,inf);%计算初始误
while s>=e %while循环开始
for i=1:3 %for循环开始
y(i)=(b(i)-h(i,:)x+h(i,i)x(i))/h(i,i);
end %for循环结束
s=norm(x-y,inf);%计算结束时的误
x=y;%得出结果x
end % s 可以参考MATLAB语言常用算法程序集 SOR迭代法AX=b function [x,n]=SOR(A,b,x0,w,eps,M) if nargin==4 eps= 1.0e-6; M = 200; elseif nargin<4 error return elseif nargin ==5 M = 200; end if(w<=0 || w>=2) error; return; end D=diag(diag(A)); %求A的对角矩阵 L=-tril(A,-1); %求A的下三角阵 U=-triu(A,1); %求A的上三角阵 B=inv(D-Lw)((1-w)D+wU); f=winv((D-Lw))b; x=Bx0+f; n=1; %迭代次数 while norm(x-x0)>=eps x0=x; x =Bx0+f; n=n+1; if(n>=M) disp('Warning: 迭代次数太多,可能不收敛!'); return; end end 块雅克比迭代法求线性方程组Ax=b的解 function [x,N]= BJ(A,b,x0,d,eps,M) if nargin==4 matlab解方程组的雅克比矩阵 求解线性方程组的雅克比迭代 eps= 1.0e-6; M = 10000; elseif nargin<4 error return elseif nargin ==5 M = 10000; %参数的默认值 end NS = size(A); n = NS(1,1); if(sum(d) ~= n) disp('分块错误!'); return; end bnum = length(d); bs = ones(bnum,1); for i=1:(bnum-1) bs(i+1,1)=sum(d(1:i))+1; %获得对角线上每个分块矩阵元素索引的起始值 end DB = zeros(n,n); for i=1:bnum endb = bs(i,1)+d(i,1)-1; DB(bs(i,1):endb,bs(i,1):endb)=A(bs(i,1):endb,bs(i,1):endb); %求A的对角分块矩阵 end for i=1:bnum endb = bs(i,1)+d(i,1)-1; invDB(bs(i,1):endb,bs(i,1):endb)=inv(DB(bs(i,1):endb,bs(i,1):endb)); %求A的对角分块矩阵的逆矩阵 end N = 0; tol = 1; while tol>=eps x = invDB(DB-A)x0+invDBb; %由于LB+DB=DB-A N = N+1; %迭代步数 tol = norm(x-x0); %前后两步迭代结果的误 x0 = x; if(N>=M) disp('Warning: 迭代次数太多,可能不收敛!'); return; end end 高斯-赛德尔迭代法求线性方程组Ax=b的解 function [x,N]= BJ(A,b,x0,d,eps,M) if nargin==4 eps= 1.0e-6; M = 10000; elseif nargin<4 error return elseif nargin ==5 M = 10000; %参数的默认值 end NS = size(A); n = NS(1,1); if(sum(d) ~= n) disp('分块错误!'); return; end bnum = length(d); bs = ones(bnum,1); for i=1:(bnum-1) bs(i+1,1)=sum(d(1:i))+1; %获得对角线上每个分块矩阵元素索引的起始值 matlab解方程组的雅克比矩阵 求解线性方程组的雅克比迭代 end DB = zeros(n,n); for i=1:bnum endb = bs(i,1)+d(i,1)-1; DB(bs(i,1):endb,bs(i,1):endb)=A(bs(i,1):endb,bs(i,1):endb); %求A的对角分块矩阵 end for i=1:bnum endb = bs(i,1)+d(i,1)-1; invDB(bs(i,1):endb,bs(i,1):endb)=inv(DB(bs(i,1):endb,bs(i,1):endb)); %求A的对角分块矩阵的逆矩阵 end N = 0; tol = 1; while tol>=eps x = invDB(DB-A)x0+invDBb; %由于LB+DB=DB-A N = N+1; %迭代步数 tol = norm(x-x0); %前后两步迭代结果的误 x0 = x; if(N>=M) disp('Warning: 迭代次数太多,可能不收敛!'); return; end end 由于方程个数3>未知数个数2,且涉及到距离的计算,所以原问题是求解一个超定(非线性)方程组.平方和形式这里采用最小二乘法解决(n个定点与未知点的计算,n>=2): 先建立pfun.m文件: %给出待求点p(x,y)的初值p0 %n个定点的坐标x0,y0 %中间相邻两定点到p的距离(一个点与起点求) function[p,norm,res,exit,out]=pfun(p0,x0,y0,det) %判断det输入的正确性 n=length(det); fori=1:n-1 ifabs(det(i))>sqrt((x0(i)-x0(i+1))^2+(y0(i)-y0(i+1))^2); error('abs(det)isgreaterthanthedisdanceoftwopoint') end end ifabs(det(n))>sqrt((x0(n)-x0(1))^2+(y0(n)-y0(1))^2) error('abs(det)isgreaterthanthedisdanceoftwopoint') end %下降算法采用Lnberg-Marquardt法 %函数调用次数为1000 %由用户定义目标函数的雅可比矩阵 opt1=optimset('LargeScale','off','MaxFunEvals',1000,'Jacobian','on'); %用非线性最小二乘命令求p(x,y),x=p(1),y=p(2) [p,norm,res,exit,out]=lsqnonlin(@dfun,p0,[],[],opt1,x0,y0,det); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %建立含雅可比矩阵的pfun.m文件 function[f,g]=dfun(p,x0,y0,det) n=length(det); fori=1:n-1 f(i)=sqrt((p(1)-x0(i))^2+(p(2)-y0(i))^2)-... sqrt((p(1)-x0(i+1))^2+(p(2)-y0(i+1))^2)-det(i); end f(n)=sqrt((p(1)-x0(n))^2+(p(2)-y0(n))^2)-... sqrt((p(1)-x0(1))^2+(p(2)-y0(1))^2)-det(n); %当函数用两个输出参数调用时 ifnargout>1 %计算雅可比矩阵 fori=1:n-1 g(i,1)=(p(1)-x0(i))/sqrt((p(1)-x0(i))^2+(p(2)-y0(i))^2)-... (p(1)-x0(i+1))/sqrt((p(1)-x0(i+1))^2+(p(2)-y0(i+1))^2); g(i,2)=(p(2)-y0(i))/sqrt((p(1)-x0(i))^2+(p(2)-y0(i))^2)-... (p(2)-y0(i+1))/sqrt((p(1)-x0(i+1))^2+(p(2)-y0(i+1))^2); end g(n,1)=(p(1)-x0(n))/sqrt((p(1)-x0(n))^2+(p(2)-y0(n))^2)-... (p(1)-x0(1))/sqrt((p(1)-x0(1))^2+(p(2)-y0(1))^2); g(n,2)=(p(2)-y0(n))/sqrt((p(1)-x0(n))^2+(p(2)-y0(n))^2)-... (p(2)-y0(1))/sqrt((p(1)-x0(1))^2+(p(2)-y0(1))^2); end 例如各点关系如下: 输入: x0=[0.5,0,0]; y0=[2+sqrt(3)/2,2,0]; det=[sqrt(3)-2,2-2sqrt(2),2sqrt(2)-sqrt(3)]; p0=[1,1]; [p,norm,res,exit,out]=pfun(p0,x0,y0,det) 输出: p= 2.0000 2.0000 syms x y; %注意是syms f = exp(x^2+y^2); df_dx = diff(f, x); df_dy = diff(f, y); 求x=0.5, y=0.5处的f对y的偏导数: res_df_dy = subs(df_dy, {x, y}, {0.5,0.5}); 同理可求对x的偏导数的值。 matlab解方程组的雅克比矩阵 求解线性方程组的雅克比迭代 海森矩阵的求法(雅可比): Hf=jacobian(jacobian(f)); Hf=(Hf) function [x,n]=Jacobi_Solve(A,b,x0,dalt) % Jacobi 跌代法解线性方程组 %[x,n]=Jacobi_Solve(A,b,x0,dalt) % A 方程组系数 % b 常数项(列向量) % x0 初始值,默认为 0 % dalt 精度,默认为 10 % x 返回跌代结果 % n 返回跌代次数 e=1; i=0; r=size(b);%将矩阵b的行数及列数赋值给r a=b; if nargin<4 %输入参数个数<4 dalt=1e-8; end if nargin<3 x=zeros(r);%创建一个r行全0的矩阵 else x=x0; end r=r(1); for t=1:r a(t)=A(t,t);%选出主对角线上的元素 A(t,t)=0; A(t,:)=A(t,:)/a(t); end b=b./a; while e>=dalt Y=b-Ax; e=max(abs(Y-x)); x=Y; i=i+1; end if nargout>1 %函数输出变量数的个数>1 n=i; end 望采纳! 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至836084111@qq.com 举报,一经查实,本站将立刻删除。用Matlab写的雅各比i和高斯塞德尔以及SOR迭代法
matlab解带参数的非线性方程组
matlab中怎么求雅各比矩阵
怎么用matlab求线性方程组的雅可比迭代矩阵