How to plot same function from different starting positions

So I want to plot a function of temperature over time, however I want to see how the temperature changes when it's at different starting points going from 0 to 28 going in steps of 2.
Is there a simple way to change the starting point and have it all plot on the same graph without having to rewrite the function out multiple times?
This is the code:
C = 51
absorbed_solar_radiation = 239.4 ;
A = 221.2 ;
B = -1.3 ;
greenhouse_effect_PI = 0
dt = 1 ;
T0 = 5 ; %This is the starting temperature I want to change
T(1) = T0 ;
t0 = 0 ;
t(1) = t0 ;
n_step = 200 ;
for n = 1:n_step ;
t(n+1) = t(n) + dt;
T(n+1) = T(n) + (dt/C)*(absorbed_solar_radiation - (A - B*T(n)) + greenhouse_effect_PI) ;
end
plot(t,T, 'b.-')
xlabel('years')
ylabel('temperature')
The temperature should always tend to 14. So my question is, is there a neat way to change 'T0' without having to list all the starting points as different variables and have to write out:
T(n) + (dt/C)*(absorbed_solar_radiation - (A - B*T(n)) + greenhouse_effect_PI) ;
multiple times?

 Accepted Answer

C = 51 ;
absorbed_solar_radiation = 239.4 ;
A = 221.2 ;
B = -1.3 ;
greenhouse_effect_PI = 0 ;
dt = 1 ;
n_step = 200 ;
T0 = 5:2:20 ; % possile values of T0
m = length(T0) ; % length of T0
T = zeros(m,n_step) ; % initialize the required data
t0 = 0 ;
t = t0:dt:n_step-1 ; % time step
for i = 1:m % loop for each T
T(i,1) = T0(i) ; % pick the initial value
for n = 1:n_step-1
% calculate T and store in the data
T(i,n+1) = T(i,n) + (dt/C)*(absorbed_solar_radiation - (A - B*T(i,n)) + greenhouse_effect_PI) ;
end
end
plot(t,T)
xlabel('years')
ylabel('temperature')
legend(num2str(T0'))

More Answers (1)

Thank you so much!
I understand that you've made T an 'm' by 'n_step' matrix of zeros but would it be possible for you to explain what you've done here:
for i = 1:m
T(i,1) = T0(i) ;
And here:
for n = 1:n_step ;
T(i,n+1) = T(i,n) + (dt/C)*(absorbed_solar_radiation - (A - B*T(i,n)) + greenhouse_effect_PI) ;
Again, many thanks!

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 6 Jan 2021

Commented:

on 6 Jan 2021

Community Treasure Hunt

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

Start Hunting!