# How to plot a function with different values

조회 수: 45 (최근 30일)
Raz Omar 2019년 11월 24일
댓글: TADA 2020년 10월 29일
(M)/1+.5*exp(-k*M*t)*(M-2)
this is the funtuion I want to plot with diffrent M values M= [1,3,5]
clear all
M= 1;
k=0.1;
t=[0:24];
SS= (M)/1+.5*exp(-k*M*t)*(M-2);
plot(SS,'r-');
xlabel('Time IN [SEC]');
ylabel('S');
title('S vs Time')
legend('M1');
I only managed to plot it once with M1
how can I plot it it with 3 lines in one plot command ?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Rik 2019년 11월 24일
Have you read the documentation for plot? It contains examples of how you van plot multiple lines at the same time. Your label is also incorrect: you aren't actually plotting against time, you're plotting against the element index.

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

### 채택된 답변

TADA 2019년 11월 24일
편집: TADA 2019년 11월 24일
You can make M a column vector, then calculate S as a matrix
then if you plot it against t which is a row vector plot should treat each row of S as a separate series
M= [1;3;5];
k=0.1;
t=[0:24];
SS= (M)/1+.5*exp(-k*M*t).*(M-2);
plot(t, SS, '-');
legend(strcat('M', num2str(M)));
another approach would be to plot in a loop and use hold on:
for M = [1,3,5]
SS = (M)/1+.5*exp(-k*M*t)*(M-2);
plot(t, SS, '-', 'DisplayName', ['M' num2str(M)]);
hold on;
end
hold off;
legend show;
##### 댓글 수: 2없음 표시없음 숨기기
Ana Venerio 2020년 10월 29일
Thank you so much, this was so helpful :)
TADA 2020년 10월 29일
Cheers!

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

### 추가 답변 (2개)

Pavel Osipov 2019년 11월 24일
Is exp(-k*M*t)*(M-2) matrix multiplication? Or exp(-k*M*t)*(M-2) element by element?
If element by element:
clear all
M = [1;3;5];%3x1 double
k=0.1;
t=0:24;%1x25 double
Wrk=M.*t;%3x25 double
Wrk1=exp(-k*Wrk);%3x25 double
Wrk2=M+0.5*Wrk1;%3x25 double
M2=M-2;%3x1 double
MM2=repmat(M2,1,length(t));%3x25 double
SS= Wrk2.*MM2;%[%%3x25 double]x[%3x25 double] element by element
figure('color','w')
plot(t,SS(1,:),'r-',t,SS(2,:),'g-',t,SS(3,:),'b-');
grid on;
xlabel('Time IN [SEC]');
ylabel('S');
title('S vs Time')
legend('M1','M2','M1');
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Raz Omar 2019년 11월 24일
편집: Raz Omar 2019년 11월 24일
Thnkas for the answer , I think it's not element by element as you said , you have used codes outside of my course , thanks for the new info

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

Pavel Osipov 2019년 11월 24일
If M2xWrk2 is matrix mult:
%If M2xWrk2 is matrix mult
clear all
M = [1;3;5];%3x1 double
k=0.1;
t=0:24;%1x25 double
Wrk=M.*t;%3x25 double
Wrk1=exp(-k*Wrk);%3x25 double
Wrk2=M+0.5*Wrk1;%3x25 double
M2=M-2;%3x1 double
%If M2xWrk2 is matrix mult
MM2=M2';%3x25 double
% --------------------------------------------------------------------------------------------------------
%[%%1x3 * 3x25 ] double matrix product => "String x Column and Sum"
SS= MM2*Wrk2;
% --------------------------------------------------------------------------------------------------------
figure('color','w')
plot(t,SS,'b-','linewidth',2);
grid on;
xlabel('Time IN [SEC]');
ylabel('S');
title('S vs Time')
legend('M1');

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

### 카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by