[Matlab] Plotting two functions of the same variable against each other

In summary, the person is having trouble plotting two functions, Vc and Ve, against each other in MATLAB. They have not used a function m file before and are unsure if the different orders of magnitude will affect the graph. They mention having the idea to plot the vectors against each other after posting the question and thank the expert for their response.
  • #1
av8or
2
0
I need to make two plots of volume as a function of time vs pressure as a function of time. When plotted the graph will have an enclosed area. I am having trouble on how to have MATLAB plot these two functions against each other.

My experience with MATLAB is having a function defined in a script file. I have not been exposed to using a function m file to define a function and reference it in a script.

My functions are as follows where (t) is the only variable, everything else is defined earlier in the file. I need to graph P vs Vc and P vs Ve. The order of magnitudes of Vc and Ve is different by 10^-3 hopefully that won't matter.

Vc =@(t) vc*sin(omega*t);
Ve =@(t) ve*cos(omega*t);
A =@(t) Vc(t)/(R*Tc) + vtl/(R*Ttl) + vr/(R*Tr) + Ve(t)/(R*Te);
P =@(t) M/A(t);
 
Physics news on Phys.org
  • #2
av8or said:
I need to make two plots of volume as a function of time vs pressure as a function of time. When plotted the graph will have an enclosed area. I am having trouble on how to have MATLAB plot these two functions against each other.
Why not just plot the vectors against each other?
 
  • #3
Simon Bridge said:
Why not just plot the vectors against each other?

I actually had that idea shortly after posting this. Thank you for your response.
 
  • #4
No worries - sometimes the process of asking a question will produce the answer.
 
  • #5


Thank you for your question. To plot two functions of the same variable against each other in MATLAB, you can use the "plot" function. This function takes in two arrays, one for the x-values and one for the y-values, and plots them as a line graph. In your case, you can use the time values as the x-values and the output of your functions (P and Vc or Ve) as the y-values.

For example, to plot P vs Vc, you can use the following code:

t = linspace(0,10,100); % creates an array of 100 evenly spaced time values between 0 and 10
Vc_values = Vc(t); % calculates the Vc values at each time point
P_values = P(t); % calculates the P values at each time point
plot(Vc_values, P_values); % plots P vs Vc

You can do the same for P vs Ve by replacing Vc_values with Ve_values in the plot function.

If the order of magnitudes of Vc and Ve is different by 10^-3, it should not affect the plot as long as the values are within the range of the axes. If you find that the plot does not look right, you can adjust the axes limits using the "xlim" and "ylim" functions.

I hope this helps and let me know if you have any further questions. Good luck with your project!
 

1. How do I plot two functions in Matlab?

To plot two functions in Matlab, you can use the "plot" function. Simply enter the two functions as arguments, separated by a comma, and the plot will appear on the graph.

2. Can I plot two functions with different variables in Matlab?

Yes, you can plot two functions with different variables in Matlab. Simply define the variables before plotting the functions and use them in the function expressions.

3. How can I change the colors or styles of the plotted functions?

You can use the "plot" function with additional arguments to specify the color, line style, and marker of the plotted functions. For example, "plot(x, y, 'r--o')" will plot a red dashed line with circles as markers.

4. How do I add a legend to a plot with two functions?

To add a legend to a plot with two functions, use the "legend" function. You can specify the labels for each function as arguments, and they will appear in the legend.

5. Can I plot more than two functions in the same graph?

Yes, you can plot multiple functions in the same graph by using the "hold on" command before each "plot" command. This will allow you to add multiple plots to the same graph without overwriting the previous one.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
985
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
928
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top