Plotting multiple functions in the same graph

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?

 채택된 답변

Walter Roberson
Walter Roberson 2020년 4월 14일

0 개 추천

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

Seems to be the second one is incorrect; also I am getting expected plot with my proposed code.
For unequal interval how I can rewrite the second code? For example, say I want to plot first function from [0 2] and second function from [2 5]. @walter Roberson.
syms y
t=[1 5];
for i=1:length(t)
U=exp(-y)*sin(t(i));
fplot(U,[0,2]); hold on;
U1=exp(-y)*cos(t(i));
fplot(U1,[2,5]);
end
hold off
Moslem Uddin
Moslem Uddin 2020년 4월 21일
편집: Moslem Uddin 2020년 4월 22일
Is there any other way performing this plotting without using for loop? For loop seems to reducing Matlab's performance. @walter roberson
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().

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

제품

릴리스

R2016a

태그

질문:

2020년 4월 14일

댓글:

2020년 4월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by