How do I plot a function with a variable that has multiple values?
Show older comments
I am new to Matlab and I am trying to plot the following function where T0 needs to have multiple values:
k=1;
T0=20;
Tm=25;
t=linspace(0,30,100);
T=Tm+(T0-Tm)*exp(-k*t);
plot(t,T)
axis([0 30 0 60])
I have been trying to assign multiple values for T0 but I get an error every time. Can anyone help?
Answers (1)
Star Strider
on 9 Sep 2017
You can create a matrix using a vector for ‘T0’ and the exponential function in ‘t’ using vector multiplication, then plot it as a function of ‘t’. For best results, the ‘t’ and ‘T0’ vectors have different lengths.
See if this does what you want:
k=1;
T0=linspace(20, 25, 5)'; % Column Vector Of ‘T0’ Values
Tm=25;
t=linspace(0,30,100); % Row Vector Of ‘t’ Values
T=Tm+(T0-Tm)*exp(-k*t);
plot(t,T)
axis([0 30 0 60])
Choose the range and number of values for ‘T0’.
The rest of your code is unchanged.
2 Comments
Tyler Kelly
on 9 Sep 2017
Star Strider
on 9 Sep 2017
As always, my pleasure!
If my Answer solved your problem, please Accept it!
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!