solving a system of non-linear equations using Fixed point method

24 Ansichten (letzte 30 Tage)
This is my question
Implement the Fixed-point method for solving a system of non-linear equations from scratch in MAT LAB and walk me through your thought process in constructing the code. Additionally, demonstrate that your implementation works by applying it to the following system. x^2 +y^2 +z^2 = 14, x^2−y^2= 2, x +y+z =4.
%ingredients
g = input(' Enter your function ');
x0 = input(' Enter initiak guess '); %x0=0
e = input(' Enter tolerance '); %10^(-4)
n = input(' Enter number of iterations '); %n=20
for i=1:n
x1 = g(x0);
fprintf(' x%d = %.4f\n ' ,i,x1)
if abs (x1-x0)<e
break
end
x0 = x1;
end
This is my code related to this question. Is this correct Please kindly pointout my mistakes...
  2 Kommentare
Torsten
Torsten am 17 Sep. 2024
What is the function you want to apply the fixed-point method to ? You didn't specify it in your code.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rishav
Rishav am 17 Sep. 2024
Hi Kumuthu,
The original code snippet was designed for a single equation, not a system of equations.
For the system of equations you have shared, we need to express each variable in terms of the others to use the Fixed-point iteration.
We can reformulate the shared equations to isolate each variable:
  • x = sqrt (2 + y^2)
  • y = sqrt (x^2 - 2)
  • z = 4 - x - y
Please refer to the following implementation for the same:
function fixed_point_method()
% Define 'g' for the system of equations
g = @(x) [sqrt(2 + x(2)^2); sqrt(x(1)^2 - 2); 4 - x(1) - x(2)];
% Example initial guess
x0 = [0; 0; 0];
% Tolerance and maximum number of iterations
e = 1e-4;
n = 20;
% Fixed-point iteration
for i = 1:n
x1 = g(x0);
fprintf('Iteration %d: x = %.4f, y = %.4f, z = %.4f\n', i, x1(1), x1(2), x1(3));
% Check for convergence
if norm(x1 - x0) < e
fprintf('Converged to solution: x = %.4f, y = %.4f, z = %.4f\n', x1(1), x1(2), x1(3));
break;
end
% Update the guess
x0 = x1;
end
% If did not converge
if i == n
fprintf('Did not converge within %d iterations.\n', n);
end
end
To know more about '@x', please refer to the following MATLAB Answers post:
  3 Kommentare
Kumuthu
Kumuthu am 17 Sep. 2024
@Rishav thank you very much and I got my mistakes.
Torsten
Torsten am 17 Sep. 2024
For the system of equations you have shared, we need to express each variable in terms of the others to use the Fixed-point iteration.
That's not necessary. If you have a system of equations F(x) = 0, write it as x = F(x) + x, and you can apply fixed-point iteration. Whether it converges is a second question.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by