Help with Matlab: Solving System of Equations

In summary, the person is seeking help with understanding how to set up a matrix and vector to solve a system of equations using the gauss seidel method in Matlab. They have attached a copy of their gauss seidel function and are unsure of how to approach part (a) of the question.
  • #1
Benjamin#1
1
0
Help with Matlab!

Hi,
I've just started learning how to use the Matlab and came across this question in the textbook:
Consider the following system of equations:
[-1 2 4 1 0]
[ 5 4 0 0 0]
[ 0 6 1 0 4] x =
[ 0 -1 0 -1 4]
[ 1 0 -2 -5 1]

[ 1 ]
[ 1 ]
[ 1 ]
[ 1 ]
[ 1 ]


Write a Matlab script file, using the Matlab function gauss seidel that:
(a) Sets up the matrix and the vector in the system of equations
(b) Applies 5 iterations of the gauss seidel method using x(0)=[0,0,0,0,0]T

Now I know how to part (b) but can't seem to get my head around part (a). I have attached a copy of the gauss seidel function I wrote. I know for part (b) I have to put in the values of the inputs: A,b,y,N and then simply execute the function. I really don't know how to solve part (a). Help Please!:cry:

Edit to say: I honestly don't really understand part (a) of the question. If they are simply asking me to write up the matrix and the vector to calculate the x vector then I know how to do that but I don't know if the question is asking me to actually set it up as x1+x2+x3+...=some no.
If so, do I simply create an M-file like the one below?
A=[-1 2 4 1 0;5 4 0 0 0;0 6 1 0 4;0 -1 0 -1 4;1 0 -2 -5 1]
b=[1;1;1;1;1]
x = A\b

Gauss Seidel function:
function x = gs (A,b,y,N)

n = length(y);

x = y;

for k=1:N
for i=1:n
sum = b(i);
for j=1:i-1
sum = sum - A(i,j)*x(j);
end
for j = i+1:n
sum = sum - A(i,j)*y(j);
end
x(i) = sum/A(i,i);
end
y = x';
end
 
Last edited:
Physics news on Phys.org
  • #2


What an opportune moment to have joined!

From my reading of part a, your interpretation is correct. Make a matrix of coefficients (what you have as A) and a vector of equation solutions (what you have as b). This allows you to use it with part b. Sometimes questions really are as easy as you think they are. ;-)
 
  • #3

end

Hi there,

First of all, don't worry if you're having trouble with part (a) of the question. It's completely normal to struggle with certain aspects of programming or math while learning something new.

To answer your question, it seems like the first part of the question is asking you to set up the matrix and vector in the system of equations. This means that you need to create a matrix A and a vector b that represent the equations given in the problem.

In the matrix A, each row represents an equation, and each column represents a variable. So the first row of A would represent the equation -x1+2x2+4x3+x4=0. Similarly, the first element of the vector b would represent the constant term in the first equation, which is 0 in this case.

To create the matrix and vector, you can follow the same format as the one you used in your Gauss Seidel function. So your M-file for part (a) would look something like this:

A = [-1 2 4 1 0; 5 4 0 0 0; 0 6 1 0 4; 0 -1 0 -1 4; 1 0 -2 -5 1];
b = [0; 0; 0; 0; 0];

Note that I replaced the last row of the matrix and the vector with 0s, as they represent the equation [1 1 1 1 1]x=0, which is not included in the given system of equations.

I hope this helps! Keep practicing and don't hesitate to ask for help if you get stuck again. Good luck!
 

1. How do I solve a system of equations in Matlab?

To solve a system of equations in Matlab, you can use the "solve" function. This function takes in the equations and the variables as inputs and returns the solutions in a symbolic form. If you want numerical solutions, you can use the "fsolve" function.

2. Can I solve a system of equations with complex numbers in Matlab?

Yes, Matlab has built-in support for complex numbers. You can use the "solve" function as usual, and Matlab will automatically handle the complex solutions.

3. How do I input a system of equations in Matlab?

You can input a system of equations in Matlab using symbolic variables and the "syms" command. This will allow you to use algebraic expressions in your equations. Alternatively, you can input the equations as strings using the "sym" function.

4. Is there a limit to the number of equations I can solve in Matlab?

No, there is no limit to the number of equations you can solve in Matlab. However, as the number of equations increases, the computation time may also increase, so it is important to consider the efficiency of your code.

5. Can I solve a system of non-linear equations in Matlab?

Yes, Matlab can solve systems of non-linear equations using the "solve" or "fsolve" function. However, these methods may not always find a solution depending on the complexity of the equations and the starting values provided. It is important to check the results for accuracy and consider using different methods if needed.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
571
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
Back
Top