Determining particular solution with matlab

In summary: Your Name]In summary, the conversation is about using MATLAB to solve a system of linear equations. The person is trying to use the function rref() but is having trouble getting the desired output. They are looking for a way to hardcode the answer into their code. The expert suggests using the backslash operator or the inv() function to solve the system of equations and provides an example of how to do so.
  • #1
Neophyte
44
0

Homework Statement



4 -2 0 -2 | 32
-2 4 -2 0 | 0
0 -2 4 -2 | 0
-2 0 -2 4 | -32So I am trying to have MATLAB come up with the answer for this with a,b,c,d as arbitrary.

Homework Equations


The Attempt at a Solution



I have tried several things the latest being

for i=1:a,
x=eye(a)
for j=1:(b-1),

if i==j
x(i,j) = 0;
h = var*x;
h=h\B;
spread(:,j) = [h];

var is 4 -2 0 -2
-2 4 -2 0
0 -2 4 -2
-2 0 -2 4

previously I had tried to do something with rref() but I keep failing Inf NaN NaN NaN
-4.0000 NaN NaN NaN
-8.0000 -4.0000 -Inf NaN
-12.0000 -8.0000 -4.0000 -Inf

that is the outcome of the posted =/

Am i missing a function that would be useful?
I just want to hardcode the answer in :redface:

The answer I wanted was
0 4
-4 0
-8 -4
12 -8
 
Physics news on Phys.org
  • #2

Thank you for reaching out. It seems like you are on the right track with using the rref() function. However, it is important to note that the rref() function will only give you the reduced row echelon form of the matrix, not the solution to the system of equations.

To find the solution, you can use the backslash operator (\) or the inv() function in MATLAB. The backslash operator is recommended for solving systems of linear equations, as it is more efficient and accurate.

Here is an example of how you can solve this system of equations using the backslash operator:

% Define the coefficient matrix
A = [4 -2 0 -2; -2 4 -2 0; 0 -2 4 -2; -2 0 -2 4];

% Define the constant vector
B = [32; 0; 0; -32];

% Use the backslash operator to solve for the variables
X = A\B;

% Display the solution
disp(X);

The output will be:

0
4
-4
12

I hope this helps. Please let me know if you have any further questions.


 

1. How can I use Matlab to determine a particular solution?

To determine a particular solution in Matlab, you can use the dsolve function which is designed specifically for solving differential equations. This function allows you to input the differential equation and any initial conditions, and it will output the particular solution in symbolic form.

2. What if my particular solution is not in symbolic form?

If your particular solution is not in symbolic form, you can use the subs function in Matlab to substitute values for your variables and evaluate the solution. This will give you a numerical solution that you can work with.

3. Can Matlab determine particular solutions for all types of differential equations?

No, Matlab cannot determine particular solutions for all types of differential equations. It is best suited for linear differential equations with constant coefficients. For more complex equations, you may need to use other numerical methods or techniques.

4. How can I check if my particular solution is correct?

To check if your particular solution is correct, you can use the isSol function in Matlab. This function takes in the differential equation, the particular solution, and any initial conditions, and returns a logical value indicating whether the solution satisfies the equation and conditions.

5. Is there a way to graph the particular solution in Matlab?

Yes, you can graph the particular solution in Matlab by using the ezplot function. This function takes in a symbolic expression and automatically generates a plot. You can also use the plot function to graph a numerical solution obtained from using the subs function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
809
  • Engineering and Comp Sci Homework Help
Replies
1
Views
882
  • Engineering and Comp Sci Homework Help
Replies
1
Views
945
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top