How to Draw Two Graphs on the Same Axis in MATLAB?

In summary, the conversation is about drawing two graphs in MATLAB using a function file. The graphs are of two equations involving arbitrary parameters A, m, and gamma, and with specific values for omega and omega_0. The time vector is set to be evenly spaced between 0 and 6pi and the two functions are plotted on the same axis using the command plot(t, func1, t, func2).
  • #1
MathematicalPhysicist
Gold Member
4,699
371
I need to draw two graphs in the same axis in matlab, and I'm novice in matlab, so if someone can type the code for this it will be much appreciated, this is the only way I can learn right now.
the graphs are of:
[tex]\frac{1}{2}m\omega^2A^2e^{-\gamma t}[1+\frac{\gamma}{2\omega}sin(2(\omega t+\phi)+\frac{\gamma^2}{2\omega}cos^2(\omega t+ \phi)][/tex]
and [tex]\frac{1}{2}m\omega^2A^2e^{-\gamma t}[/tex]
where A,m and gamma are arbitrary parameters, and [tex]\omega_0/\gamma=10[/tex]
and [tex]\omega=\sqrt{\omega_0^2-1/4\gamma^2}[/tex]
I need that the graph will span over a number of periods.

As I said I'm novice and not sure how to handle this, and reading the help may take more time for me.

thanks in advance.
 
Physics news on Phys.org
  • #2
Set your time vector to be t=linspace(0,6*pi,200) which will give you a vector of 200 points evenly spaced between 0 and [itex] 6 \pi [/itex]. We can adjust this later if you wish.

Write a function file with the outputs as your two funcitons above. To plot them on th esame axis, the command is plot(t, func1, t, func2); which will give you two lines.

if you don't get it, I wrote a couple of lines of code to help.
 
Last edited:
  • #3


Hello,

Thank you for reaching out. Drawing graphs in MATLAB can seem daunting at first, but with practice, you will become more comfortable with it.

To draw two graphs in the same axis, you can use the "hold on" and "hold off" commands. This will allow you to plot multiple graphs on the same axis without overwriting the previous one.

To plot the first graph, you can use the "plot" command with the equation provided. For example, you can use the following code:

hold on
A = 1; % arbitrary parameter
m = 1; % arbitrary parameter
gamma = 1; % arbitrary parameter
omega_0 = 10*gamma; % given condition
omega = sqrt(omega_0^2-1/4*gamma^2); % given condition
phi = pi/4; % arbitrary parameter
t = 0:0.01:10; % time range for plotting
y1 = 1/2*m*omega^2*A^2*exp(-gamma*t).*(1+gamma/2/omega*sin(2*(omega*t+phi))+gamma^2/2/omega*cos(omega*t+phi).^2); % equation for first graph
plot(t,y1) % plot the graph
hold off

For the second graph, you can use the same code, but replace "y1" with "y2" and use the equation for the second graph. The final code will look like this:

hold on
A = 1; % arbitrary parameter
m = 1; % arbitrary parameter
gamma = 1; % arbitrary parameter
omega_0 = 10*gamma; % given condition
omega = sqrt(omega_0^2-1/4*gamma^2); % given condition
phi = pi/4; % arbitrary parameter
t = 0:0.01:10; % time range for plotting
y1 = 1/2*m*omega^2*A^2*exp(-gamma*t).*(1+gamma/2/omega*sin(2*(omega*t+phi))+gamma^2/2/omega*cos(omega*t+phi).^2); % equation for first graph
y2 = 1/2*m*omega^2*A^2*exp(-gamma*t); % equation for second graph
plot(t,y1) % plot the first graph
plot(t
 

1. How do I plot a graph using Matlab?

To plot a graph using Matlab, first import your data into the program. Then, use the plot() function to create a basic graph. You can customize your graph further by adding labels, titles, legends, and adjusting the axes.

2. Can I plot multiple graphs on the same plot using Matlab?

Yes, you can plot multiple graphs on the same plot using Matlab. Simply use the hold on command before each additional plot() function to prevent the previous graph from being overwritten. You can also use the subplot() function to create multiple plots within one figure.

3. How do I change the color or style of my graph in Matlab?

To change the color or style of your graph in Matlab, you can specify the desired color or style within the plot() function. For example, plot(x,y,'r--') will plot a red dashed line. You can also use the color and linestyle commands to change the color and style of an existing plot.

4. Is it possible to add error bars to a graph in Matlab?

Yes, it is possible to add error bars to a graph in Matlab. After creating your plot using the plot() function, you can use the errorbar() function to add error bars to your data points. You can also customize the appearance of the error bars using optional arguments.

5. Can I save my graph as an image or export it to another program in Matlab?

Yes, you can save your graph as an image or export it to another program in Matlab. Use the saveas() function to save your plot as an image file, such as PNG or JPEG. To export your graph to another program, you can use the export_fig function, which allows you to save in various file formats and adjust the resolution and size of the exported graph.

Similar threads

  • Introductory Physics Homework Help
Replies
18
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
144
  • Advanced Physics Homework Help
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Classical Physics
Replies
17
Views
1K
  • Introductory Physics Homework Help
Replies
17
Views
377
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top