本帖最后由 matlab的旋律 于 2018-12-17 10:07 编辑
项目需求如下:
使用牛顿法解方程组.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%主函数代码 main.m clear
close all
clc
t0=[470 200]; % 迭代初始值
eps = 1e-6; % 定位精度要求
for i = 1:10
f = double(subs(Function(t0),{'t2' 't3'},{t0(1) t0(2)}));
df = double(subs(deFunction(t0),{'t2' 't3'},{t0(1) t0(2)})); % 得到雅克比矩阵
t = t0 - f/df;
if(abs(t-t0) < eps)
break;
end
t0 = t; % 更新迭代结果
end
f = double(subs(Function(t0),{'t2' 't3'},{t0(1) t0(2)}));
disp(['定位坐标',num2str(t)]);
disp(['迭代次数:',num2str(i)]);
disp(['方程组的值:',num2str(f)])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%定义非线性方程组如下
function f = Function(t)
%变量t1 t2
%函数f1 f2
syms t2 t3
f1 = -191*t2^2/4000000 - 203471*t2/2000000 + 23*t3^2/2000000 + 10971*t3/2000000 + 1808829/32000;
f2 = 7*t2^2/1000000 + 3339*t2/1000000 - 7*t3^2/100000 - 18339*t3/1000000 + 9/10;
f=[f1 f2];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%雅克比矩阵
function df = deFunction(t)
f = Function(t);
df = [diff(f,'t2');diff(f,'t3')]; %雅克比矩阵
|