Sei sulla pagina 1di 3

8/21/2018 Jacobi iterative method in matlab - MATLAB Answers - MATLAB Central

MATLAB Answers™
Related Content
MATLAB Answers

Jacobi method in
MATLAB
Flag
 Vote
Follow 1 Answer
0

Jacobi iterative method in matlab how to display iterations


of linear equations at
Asked by ribagorza on 7 Oct 2014
Latest activity Edited by Rafid Jabbar on 15 May 2017
each step size?
429 views (last 30 days)
1 Answer
I just started taking a course in numerical methods and I have an assignment to code
the Jacobi iterative method in matlab. So this is my code (and it is working):
How to avoid using 'diag'
function?
function x1 = jacobi2(a,b,x0,tol)
n = length(b); 1 Answer
for j = 1 : n
x(j) = ((b(j) - a(j,[1:j-1,j+1:n]) * x0([1:j-1,j+1:n])) / a(j,j)); %
Entire Website
end
x1 = x'; Creating Sparse Finite-
k = 1; Element Matrices in
while norm(x1-x0,1) > tol
MATLAB
for j = 1 : n
x_ny(j) = ((b(j) - a(j,[1:j-1,j+1:n]) * x1([1:j-1,j+1:n])) / a(j,j)
Blogs
end
x0 = x1; Recent Question about
x1 = x_ny';
k = k + 1;
Speed with Subarray
end Calculations
k
x = x1'; Blogs

The Historic MATLAB


Users' Guide
I'm assuming there is alot I can do to make this code better since I'm new to matlab,
and I would love som feedback on that. But my question is if I instead of what I have Blogs
done should use the matrix method where we have xk+1 = inv(D) * (b - (L+U) * xk)). Is
this a more effective method? And how should I think when deciding what method to Tags
use, how do I know what method is more effective?
jacobi
If someone could help me it would be great!

Products
MATLAB
 1 Comment


 lubna ineirat on 28 Jun 2016

what we can do in the fuzzy linear system?

 Comment on this Question


https://in.mathworks.com/matlabcentral/answers/157632-jacobi-iterative-method-in-matlab 1/3
8/21/2018 Jacobi iterative method in matlab - MATLAB Answers - MATLAB Central

Four Steps to
Building Smarter
RF Systems with
MATLAB
 1 Answer
 Download white
paper


Vote
6
Flag Link
Answer by Bruno Pop-Stefanov  on 8 Oct 2014

1. Some feedback about your code

It's good practice to pre-allocate memory before a for loop. This is


actually what Code Analyzer suggests for variables x and x_ny. Since
you know that x will eventually contain n elements, I would add:

x = zeros(n,1);

before the first for loop. That way you can also control that x will be a
column vector (or a row vector if you use zeros(1,n)) and you do not
need to transpose x after the loop.

Same remark for x_ny. Add

x_ny = zeros(n,1);

before the second for loop.

You might actually be able to vectorize these for loops, if you find a
way to rewrite lines 4 and 10.

Here are some general advice for performance:

http://www.mathworks.com/help/matlab/matlab_prog/techniques-for-
improving-performance.html

...and about vectorization in particular:

http://www.mathworks.com/help/matlab/matlab_prog/vectorization.html

2. About the matrix method

I am not familiar with the Jacobi method, but I would avoid using inv.
Calculating the inverse of a matrix numerically is a risky operation
when the matrix is badly conditioned. It's also slower and less precise
than other linear solvers. Instead, use mldivide to solve a system of
linear equations. Based on how the system looks like, mldivide will
choose an appropriate method.

x(k+1) = D \ (b - (L+U)*x(k));

https://in.mathworks.com/matlabcentral/answers/157632-jacobi-iterative-method-in-matlab 2/3
8/21/2018 Jacobi iterative method in matlab - MATLAB Answers - MATLAB Central

 1 Comment


 Rafid Jabbar on 15 May 2017

Dears, Please could one answer me, how I can solve below
equation numerically by Jacobi method to get temperature
distribution along z-axis, 1D problem, steady state: (

 Comment on this Answer

Answer this question


Body
BoldItalicMonospacedBulleted listNumbered listCodeHyperlinkImageAttach fileHelp

PreviewDisable

Follow activity on this question (change notification settings)

Cancel Submit

mathworks.com
© 1994-2018 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks
of their respective holders.

https://in.mathworks.com/matlabcentral/answers/157632-jacobi-iterative-method-in-matlab 3/3

Potrebbero piacerti anche