Newton methods for solving nonlinear

3 views (last 30 days)
Khai
Khai on 15 Nov 2023
Commented: Khai on 16 Nov 2023
Please help me fix this code programe
main()
X = 4×1
22.1000 20.0000 5.0000 2.0000
Kết quả: Nhiệt độ nước lớn nhất là: 22.1
function main()
X0 = [25; 20; 4; 1];
% Áp dụng phương pháp Newton
X = newtons_method(X0)
% Hiển thị kết quả
disp('Kết quả:')
disp(['Nhiệt độ nước lớn nhất là: ', num2str(X(1))]);
end
function F = equations(X)
F = [X(1) - (X(2) + 0.5 * X(3) - 0.2 * X(4));
X(2) - 20;
X(3) - 5;
X(4) - 2];
end
function J = jacobian(X)
J = [1, -0.5, -0.1, 0.2;
0, 1, 0, 0;
0, 0, 1, 0;
0, 0, 0, 1];
end
function X = newtons_method(X0)
% Phương pháp Newton
max_iterations = 100;
tolerance = 1e-6;
X = X0;
for i = 1:max_iterations
F = equations(X);
J = jacobian(X);
delta_X = -J \ F;
X = X + delta_X;
if norm(delta_X, inf) < tolerance
break;
end
end
end
  8 Comments
Khai
Khai on 16 Nov 2023
this is my new lesson. Please help me solve it

Sign in to comment.

Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!