Plotting System of Equations in MATLAB

In summary, the conversation is about a user seeking help on plotting a system of equations in MATLAB and another user providing different methods and commands to achieve this. The conversation starts with the first user mentioning a change on the physics forums and requesting help with plotting equations. The second user responds with a sample code using the plot command and explains how it works. The first user then asks for help in plotting multiple equations, to which the second user provides another sample code using the hold command. The conversation ends with the first user expressing gratitude for the help.
  • #1
FrogPad
810
0
Maybe not the proper place to post... but it seems like physics forums is going through some type of "crazy" change right now. Anyways... I'm having trouble finding a website with directions on how to plot a system of equations in MATLAB. If anyone has a basic M-file they could show me, or a tutorial... that'd be really awesome! Thanks for the help :)
 
Physics news on Phys.org
  • #2
Code:
x=[-5:.1:5]; % x is a vector containing the numbers -5, -4.9, -4.8, ..., 4.9, 5. Semicolon means don't display to screen
y=4x.^3 - 2x.^2 - 12; % The '.'^ raises each element of the vector to the power listed. 
                                % If you just did ^ it would do matrix multiplication which doesn't work unless the matrix is square.

plot(x,y,'r*') % Plot variable X on the horizontal axis, y on the vertical, and display the points as red stars

Is that what you wanted, or did you need something more?
 
  • #3
Actually, I already knew about the plot command. What I didn't know is that you can plot multiple equations with it.

t1=0:.1:10;
t2=10:.1:20;
y1=t1.*1;
y2=t2./t1;

plot(t1,y1,'r-',t2,y2,'r2-',...

But thanks for the reply :smile:
 
  • #4
Ah, ok.

You can also skip the 'xxx' and have it automatically assign the colors.

Another way to do it is to hold the plot like so:

Code:
figure(1)

plot(x1,y1)

hold on

plot(x2,y2)
plot(x3,y3)

hold off

You can also simply type 'hold' to toggle it.
 

1. How do I plot a system of equations in MATLAB?

To plot a system of equations in MATLAB, you can use the plot function. First, define your equations as separate variables using the syms command. Then, use the ezplot function to plot each equation on the same graph. Alternatively, you can use the fplot function to plot each equation separately and then use the hold function to combine them onto one graph.

2. Can I plot a system of nonlinear equations in MATLAB?

Yes, you can plot a system of nonlinear equations in MATLAB. However, you will need to use the ezplot or fplot function with a range of values for the variables in order to see the shape of the curve. You may also need to use the ezplot3 or fplot3 functions for systems of equations in three dimensions.

3. How can I change the appearance of the plot for a system of equations in MATLAB?

You can change the appearance of the plot by using various optional arguments when calling the plot, ezplot, or fplot functions. These arguments allow you to change the color, line style, marker symbols, and other properties of the plot. You can also add a title, axis labels, and a legend using the title, xlabel, ylabel, and legend functions.

4. Is there a way to solve a system of equations in MATLAB and plot the solutions?

Yes, you can use the fsolve function to numerically solve a system of equations in MATLAB. Then, you can use the plot, ezplot, or fplot functions to plot the solutions on a graph. You can also use the hold function to plot the solutions on top of the original system of equations.

5. Can I save a plot of a system of equations in MATLAB as an image file?

Yes, you can save a plot of a system of equations in MATLAB as an image file by using the saveas function. This function allows you to specify the file format, such as PNG or JPEG, and the resolution of the image. You can also use the print function to save the plot as an image directly from the MATLAB command window.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
697
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
881
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
43K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
Back
Top