Newton methods for solving nonlinear

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 comentarios

Torsten
Torsten el 15 de Nov. de 2023
Ok, the first row of your Jacobian is wrong and your system of equations is a linear one so that you could have solved it easier than with Newton's method, but the iteration converges. So what exactly is your question ?
Khai
Khai el 15 de Nov. de 2023
This is my lesson: We have a lake and we want to determine its maximum teamperature, the temperature is T, and other affecting factors are the temperature of surrounding environment(Te), the amounts of UV rays(UV), the amounts of nutrients(N). F1(T,Te,UV,N)=T−(Te+0.5*UV−0.2*N) F2(T,Te,UV,N)=Te-20 F3(T,Te,UV,N)=UV-5 F4(T,Te,UV,N)=N-2 T_zero=25, Te_zero=20, UV_zero=4, N_zero=1
Torsten
Torsten el 15 de Nov. de 2023
Editada: Torsten el 15 de Nov. de 2023
I don't understand your lesson. If F1,...,F4 have to be 0, why are you talking about a "maximum temperature" ? T is uniquely determined by the four equations - there is no degree of freedom to maximize T.
Khai
Khai el 15 de Nov. de 2023
Oh because this lesson is created by myself, therefore it is will not suistable. If you have any ideas, please introduce for me a new one. The only condition is that using the newton method for solving nonlinear to determine the maximum temperature of a source water
Torsten
Torsten el 15 de Nov. de 2023
We have enough problems in the world - why inventing a new one ?
John D'Errico
John D'Errico el 15 de Nov. de 2023
Your "solver" works. At least it does on the trivial example problem you chose, which is purely linear, so it will "converge" in one iteration. So where is the problem?
Khai
Khai el 16 de Nov. de 2023
Khai
Khai el 16 de Nov. de 2023
this is my new lesson. Please help me solve it

Iniciar sesión para comentar.

Respuestas (0)

Productos

Versión

R2021a

Etiquetas

Preguntada:

el 15 de Nov. de 2023

Comentada:

el 16 de Nov. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by