solving a system of non-linear equations using Fixed point method
조회 수: 24 (최근 30일)
이전 댓글 표시
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
Torsten
2024년 9월 17일
What is the function you want to apply the fixed-point method to ? You didn't specify it in your code.
채택된 답변
Rishav
2024년 9월 17일
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
Torsten
2024년 9월 17일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Fixed-Point Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!