How to do a dynamic graph in matlab

In summary, the conversation revolved around creating a constantly changing plot using MATLAB, with the initial function being Sin(x) and then changing to Cos(x) and Tan(x). The person was unsure how to do this in MATLAB but eventually figured it out by using a nested for loop. They then shared the code for anyone else who may need it.
  • #1
juanitotruan77
44
0
Hi guys, so I've never used MATLAB before, and i need to make a plot that changes constantly.
Let's say that i have Sin(x) from 0 to 3pi
Then i need it to change to a Cos(x) from 0 to 3pi
then to a Tan(x).

I could do it in other languaje like Php or c, but in MATLAB i have no idea. I think it can be made with a for cycle nested inside of another for cycle.
 
Physics news on Phys.org
  • #2
Nevermind, i got it. What i really wanted was a changing sin plot. I'm going to leave it here in case anyone needs it.
Code:
f = @(x,t) 1.5*sin(10*x-5*(t/10));
linkdata on
hold on;
for t=1:50
    xi = -3*pi:pi/1000:pi;
    plot(f(xi,t));
    pause(.1);
clf('reset')
end
 
Last edited:
  • #3
Very cool!
 
  • #4
kreil said:
Very cool!

Thanks (:
 
  • #5


Hi there! Creating a dynamic graph in MATLAB is actually quite simple. You can use a for loop to continuously update the graph with new data points. Here's an example of how you could plot Sin(x), Cos(x), and Tan(x) on the same graph:

1. First, define the x-values for your plot. In this case, we'll use the range from 0 to 3pi with a step size of 0.1. You can do this by using the linspace function: x = linspace(0, 3*pi, 31);

2. Next, create a for loop to plot Sin(x) on the same graph. Inside the loop, use the plot function to plot the x-values against Sin(x): for i = 1:length(x) plot(x(1:i), sin(x(1:i))) % plots x-values from 1 to i against Sin(x) pause(0.1) % pauses for 0.1 seconds before updating the graph end

3. Repeat the same process for Cos(x) and Tan(x) using the same for loop structure. Just replace "sin" with "cos" or "tan" in the plot function.

4. You can also add a legend to your graph to label each function. Use the legend function and specify the labels for each plot: legend('Sin(x)', 'Cos(x)', 'Tan(x)')

5. Finally, you can add a title and axis labels to your graph using the title, xlabel, and ylabel functions.

And that's it! Your graph should now update dynamically with each new function plotted. I hope this helps and happy graphing!
 

1. How do I create a dynamic graph in Matlab?

To create a dynamic graph in Matlab, you can use the "plot" function to plot your data. Then, use the "hold on" and "pause" commands to update the plot with new data points at regular intervals. This will create the appearance of a dynamic graph.

2. Can I add labels and titles to my dynamic graph?

Yes, you can add labels and titles to your dynamic graph using the "xlabel", "ylabel", and "title" functions. These will appear on your graph and update accordingly as the data changes.

3. How can I change the appearance of my dynamic graph?

You can change the appearance of your dynamic graph by using additional arguments in the "plot" function, such as color, line style, and marker type. You can also use the "axis" function to change the limits and scale of your graph.

4. Is it possible to save my dynamic graph as an image or video?

Yes, you can save your dynamic graph as an image or video by using the "saveas" or "getframe" functions, respectively. These will allow you to capture the current state of your graph and save it for later use.

5. Can I make my dynamic graph interactive?

Yes, you can make your dynamic graph interactive by using the "ginput" function to allow users to click on the graph and input data points, or by creating a user interface using the "figure" and "uicontrol" functions. This will allow for a more engaging and customizable experience with your dynamic graph.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
992
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
561
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top