Iterative Solution for Nonlinear System and Comparing Updated Coefficients

I am working with a nonlinear system of equations, where the term appears in the equations. I need to solve the system iteratively and compare the coefficients calculated using the initial assumption (with at first) with the updated coefficients obtained after several iterations.
The steps of the process are as follows:
\begin{itemize}
\item \textbf{Step A:} Assume that the value of \( \left| \frac{\partial \phi_2'}{\partial r} \right| \) is known (set to 0 initially) and the coefficients \( c_n, c_n^-, d^0, d_n^+, d_n^- \) are unknown. The system of equations becomes linear and can be solved by Gaussian elimination.
\item \textbf{Step B:} After obtaining the initial solution, substitute the value of \( \left| \frac{\partial \phi_2'}{\partial r} \right| \) into the equation set and solve for all unknown coefficients, and update the value of \( \left| \frac{\partial \phi_2'}{\partial r} \right| \).
\item \textbf{Step C:} Calculate the updated unknown coefficients, and compare them with the values obtained in the previous iteration. If the difference between any coefficient is smaller than \( 10^{-3} \), stop the iteration and consider the solution as the final result. Otherwise, continue iterating, updating the value of \( \left| \frac{\partial \phi_2'}{\partial r} \right| \) in each step.
\end{itemize}
How can I implement this iterative solution in MATLAB? Specifically, how can I calculate and compare the differences between the unknown coefficients obtained in each step using the initial and updated values of ?

23 个评论

We don't know about the mathematical problem and the meaning of the variables in your assignment - thus we don't know what exercises A, B and C are about.
The forum cannot handle advanced LaTeX scripting. No everyone is familiar with the code.
When code is pasted and compiled in Crixet (the free browser-supported LaTeX editor), it displays the following:
I am trying to solve a system of linear equations iteratively in MATLAB. The goal is to update the coefficients in each iteration and check the difference between the updated and previous values.
The process is as follows:
1. Start with an initial guess for the unknown coefficients.
2. Solve the system to obtain the unknown coefficients.
3. Update the coefficients in the next iteration using the values from the previous step.
4. Calculate the difference between the updated coefficients and the previous ones.
5. If the difference between any of the coefficients is smaller than a predefined tolerance (e.g., 10^{-3}), stop the iteration and consider the solution as final. Otherwise, continue iterating.
How can I implement this in MATLAB? Specifically, how can I calculate the differences between the coefficients in each iteration and stop the loop when the difference is less than the tolerance? , I solve my system using pinv. now i want just to solve it iteratively to achieve accuracy by defining tolerence.
I am not familiar with your nonlinear system. However, to perform iterations when a predefined tolerance is specified, it is common to use either the for-loop or the while-loop approach. If you wish for help, please type out the nonlinear equations after clicking the indentation icon .
Here is the code snippet:
% Initialize variables
x = 1; % Initial guess
tolerance = 0.0001; % Predefined tolerance
maxIterations = 1000; % To prevent infinite loops
iteration = 0; % Initial iteration count
% Newton-Raphson method for square root of 2
while iteration < maxIterations
x_new = 0.5*(x + 2/x); % Update using Newton-Raphson formula
if abs(x_new - x) < tolerance % Check for convergence
break; % Exit loop if within tolerance
end
x = x_new; % Update x for next iteration
iteration = iteration + 1; % Increment iteration count
end
% Display the result
disp(['Estimated square root of 2: ', num2str(x)]);
Estimated square root of 2: 1.4142
@Javeria, Good, now put the linear equations in the code above.
But Newton–Raphson method is for nonlinear. What I want is that if we have a system of equations, i.e.,
  • ,
  • ,
  • ,
the term makes it nonlinear.
So we 1st assume that so then we have linear system and we solve this system simoultaneously for unknown .
Then we update our and then we put this updated value and again solve the system and get the value of .
Then we check for the difference between these values of and the previous ones. If our difference is less than our tolerence () then we stop our iteration, otherwise we continue.
The code snippet demonstrates the concept of performing "any" iterations when a predefined tolerance is specified. You can replace the Newton-Raphson method with the Gaussian elimination method of your choice.
However, I have heard that people use linsolve() for solving simultaneous linear equations because it employs more advanced techniques such as LU factorization, QR factorization, or Cholesky factorization, all of which are variants of Gaussian elimination.
Are you ready?
By the way, there is no solution exists in your example of linear equations. How about you apply the iterations to this system?
syms x y z
eqns = [x == 2*y + 3*z - 9,
y == 4*x + 9*z + 5,
6*z == 2*x + 4*abs(y + x) + 6]
eqns = 
eqns1 = [x == 2*y + 3*z - 9,
y == 4*x + 9*z + 5,
6*z == 2*x + 4*-(y + x) + 6]
eqns1 = 
[A1, b] = equationsToMatrix(eqns1)
A1 = 
b = 
sol1 = linsolve(A1, b)
sol1 = 
eqns1 = [x == 2*y + 3*z - 9,
y == 4*x + 9*z + 5,
6*z == 2*x + 4*(y + x) + 6]
eqns1 = 
[A2, b] = equationsToMatrix(eqns1)
A2 = 
b = 
sol2 = linsolve(A2, b)
sol2 = 
In principle, you can try as @Sam Chak told you to do for the nonlinear system. In the case given, both iteration types (fixed-point iteration and direct solving) don't converge.
x = [4 ;5; -7];
tolerance = 0.0001; % Predefined tolerance
maxIterations = 10; % To prevent infinite loops
iteration = 0; % Initial iteration count
while iteration < maxIterations
update = abs(x(1)+x(2));
x_new = [1 -2 -3;-4 1 -9;-2 0 1]\[-9;5;6+4*update];
%x_new = [0 2 3;4 0 9;2 0 0]*x + [-9;5;6+4*update];
if abs(x_new - x) < tolerance % Check for convergence
break; % Exit loop if within tolerance
end
x = x_new; % Update x for next iteration
iteration = iteration + 1; % Increment iteration count
end
x
x = 3×1
1.0e+06 * -1.0383 -1.0383 0.3461
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Maybe you don't get a solution because your system doesn't have one:
A = [1 -2 -3;-4 1 -9;-6 -4 1];
b = [-9 ; 5;6];
xsol = A\b;
xsol(2) + xsol(1) >= 0
ans = logical
0
A = [1 -2 -3;-4 1 -9;2 4 1];
b = [-9 ; 5;6];
xsol = A\b
xsol = 3×1
-2.1597 2.4118 0.6723
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
xsol(2) + xsol(1) <= 0
ans = logical
0
I have tried the modified code by @Torsten according to the steps provided by you. However, the iterations do not converge unless the initial guess for x and y are correct. Beat me, I was previously unaware that Gaussian elimination could be employed for solving nonlinear systems of equations. Could you share the source for this iterative Gaussian elimination method?
Perhaps @John D'Errico might shed light on this math problem?
%% Nonlinear System of Equations
% eqns = [x == 2*y + 3*z - 9,
% y == 4*x + 9*z + 5,
% 6*z == 2*x + 4*abs(y + x) + 6]
%% OP's Iterative Gaussian Elimination method
% initial guess
x = [-3; % xsol = -3
11/7; % ysol = 11/7 (1.5714)
30/31]; % zsol = 20/21 (0.9524)
tolerance = 0.0001; % Predefined tolerance
maxIterations = 1000; % To prevent infinite loops
iteration = 0; % Initial iteration count
while iteration < maxIterations
update = abs(x(1)+x(2));
x_new = [1 -2 -3;
-4 1 -9;
-2 0 6]\[-9;
5;
6 + 4*update];
if abs(x_new - x) < tolerance % Check for convergence
break; % Exit loop if within tolerance
end
x = x_new; % Update x for next iteration
iteration = iteration + 1; % Increment iteration count
end
disp(iteration)
1
x
x = 3×1
-3.0000 1.5714 0.9524
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Standard Method - Use fsolve
%% Nonlinear System
function F = nonlinSys(x)
F(1) = x(1) - (2*x(2) + 3*x(3) - 9);
F(2) = x(2) - (4*x(1) + 9*x(3) + 5);
F(3) = 6*x(3) - (2*x(1) + 4*abs(x(2) + x(1)) + 6);
end
%% call fsolve
x0 = [-5, 3, 2]; % initial guess
x = fsolve(@nonlinSys, x0)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x = 1×3
-3.0000 1.5714 0.9524
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Beat me, I was previously unaware that Gaussian elimination could be employed for solving nonlinear systems of equations.
Newton's method for nonlinear systems is nothing else but a sequence of linear systems that are being solved.
syms x y z
eqns = [x == 2*y + 3*z - 9,
y == 4*x + 9*z + 5,
6*z == 2*x + 4*abs(y + x) + 6]
eqns = 
sol = solve(eqns)
sol = struct with fields:
x: [2×1 sym] y: [2×1 sym] z: [2×1 sym]
[sol.x, sol.y, sol.z]
ans = 
Any updates? You can use the solve() function to solve for nonlinear equations as demonstrated by @Walter Roberson. Are you specifically required to use the Iterative Gaussian Elimination method?
Thank you, @Torsten. I understand that both Newton-Raphson method and Gaussian elimination deal with linear equations. However, they differ significantly in the approach they use.
hello , thanks for your response. However in my case the equations i have a bit complicated so that's why i just put a prototype example that what i want. In general i am solving boundary value problem where i have one term which is in absolute value and this absolute make it non linear. so in the article they mentioned that we can assume 1st this value zero then we solve our system of equations for unknowns, after that we can update the value of that absolute term and then make use of this value and again we will solve the system of equations for the unknowns then we define a tolerence i. 10^-3 so then we will check the difference between this value and the previous one if it is less than our defined tolerence then we can say this is our solution. i am just confused about this one thing that how i can fit my equations to solve iteratively with this tolerence.
okay let me show my system of equations. i have this type of equations. However this is system of equations we can turncate the sum to a finite number. in eq25 we have the absolute term which make it nonlinear. and the 1st snippet i upload is about these equations. I can write this system of equation in matrix form what i need then that how i define tolerence then solve equation and find unknown coefficients . then uppdate my abosulte term and then make use of it to solve the system again and then check for difference. If you can have provide me general one code i can make use of it for my problem. Thanks
I see that there are many infinite series in your three equations, which may exceed my current understanding. However, you may refer to the solution suggested by @Torsten approximately two months ago:
By the way, have you tried using the solve() approach as demonstrated by @Walter Roberson above?
@Sam Chak Hello sam the infinite series i know about this we can turncate it to some finite value. what i need is that how can i solve this system iteratively as the process i mentioned above. where we have define the process steps. the below steps. i know how to solve the system for unknows, but then i am confused that how i can fit loops that solve system iteratively and how to update the absoute term inside it and how to define the stopping criteria?
If you show us your code to solve the truncated system for a given value of dphi2dr, we will arrange it to make it work in an iterative manner.
I have only analytical part. which i need to solve.
I don't understand what you mean.
Do you have MATLAB code to solve for the unknowns given a value for dphi2dr (thus without iterating and adjusting dphi2dr according to equation (26)) ?
I thought that's what you meant when you wrote "i know how to solve the system for unknows, but then i am confused that how i can fit loops that solve system iteratively and how to update the absoute term inside it and how to define the stopping criteria?".
If you didn't code this part yet, you should start here: it's solving a system of linear equations.
Is your solution method based on a publication where it has been successfully applied ?
Correct me if I'm wrong, but it seems you work with the analytical solutions to partial differential equations in each subregion and try to adjust the free parameters of these infinite series solutions as to satisfy boundary and interface conditions.
I wonder whether it wouldn't have been possible (and easier) to discretize the underlying partial differential equation(s) together with the boundary conditions and interface conditions directly (i.e. without making use of the analytical solutions).
By the way: What are the partial differential equations for the velocity potential in the different subregions ? I guess this should be included in your problem definition abstract.
Hello, Yes this is my project. and in each region the potential must satisfy the laplace equation and the boundary conditions. What i can know that the procedure for solving it is outlinded at that snapshoot which i upload.

请先登录,再进行评论。

回答(0 个)

类别

提问:

2025-6-4

评论:

2025-6-16

Community Treasure Hunt

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

Start Hunting!

Translated by