Clear Filters
Clear Filters

Plotting multiple functions in the same graph

2 views (last 30 days)
I want to plot two different functions in a same graph for different values of a parameter.
I tried the following code:
clc
clear all
syms y
t=[1 5];
for i=1:length(t)
U=exp(-y)*sin(t(i));
fplot(U,y,[0,5]); hold on;
U1=exp(-y)*cos(t(i));
fplot(U1,y,[0,5]); hold on;
end
Is that alright?

Accepted Answer

Walter Roberson
Walter Roberson on 14 Apr 2020
syms y
t=[1 5];
for i=1:length(t)
U=exp(-y)*sin(t(i));
fplot(U,[0,5]); hold on;
U1=exp(-y)*cos(t(i));
fplot(U1,[0,5]);
end
hold off
Or you could
syms y
t=[1 5];
for i=1:length(t)
U=exp(-y)*sin(t(i));
U1=exp(-y)*cos(t(i));
fplot([U,U1],[0,5]);
hold on
end
hold off
  5 Comments
Moslem Uddin
Moslem Uddin on 21 Apr 2020
Edited: Moslem Uddin on 22 Apr 2020
Is there any other way performing this plotting without using for loop? For loop seems to reducing Matlab's performance. @walter roberson
Walter Roberson
Walter Roberson on 22 Apr 2020
If your t is fixed length, you can "unroll the loop", just putting all of the instructions in a row:
syms y
t=[1 5];
U=exp(-y)*sin(t(1));
fplot(U,[0,2]); hold on;
U1=exp(-y)*cos(t(1));
fplot(U1,[2,5]);
U=exp(-y)*sin(t(2));
fplot(U,[0,2]);
U1=exp(-y)*cos(t(2));
fplot(U1,[2,5]);
hold off
I think you will find that the for loop was contributing negligible time compared to the time required to do the fplot().

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!