'I think I'm expressing this summation incorrectly in MATLAB?

I'm trying to code the following sum (which is the solution for a functional DE):

enter image description here

This sum is valid for t in [nT, (n+1)T], where T is a constant number (found to be 14 for this example, but is generally just any number).

My problem is that, while I think I can code this generally, I'm confused about how to make this valid for each separate interval of time from t=0 to t=110 (final time that I need), unless I hardcode it, which is not exactly ideal.

My code is below

alpha = 0.5
T = 14

y = zeros(110,T) (110 is the length of the time I'll be plotting y against)
for n = 1:T
    y(1,n) = exp(alpha*n);
end

for i = 2:110
    for t=i*T:(i+1)*T
        j = mod(t,T);
        y(i,j+1) = y(i-1,j+1) + (((-alpha)^i)/factorial(i)) * (t-i*T)^i * exp(alpha*(t-i*T));
    end
end

I'm not super experienced in either coding or in MATLAB, but I need this for something really important that I'm working on, so could someone help me identify where my logic is going really wrong? Like this code runs, but it's not really giving me what I want, so any help would be appreciated.

Thank you!

EDIT:

I don't think I stated the full problem correctly or in enough detail before, so I'm adding a better explanation of what I needed. Also, so it's sad that there's no LaTeX support on SO but I'll state the equations for each time interval as well as possible. I wanted to make a code that calculates them all by using a single loop/function, but I can't think of doing this in any way that's not multiple unnecessary lines of code.

Basically;

when t is in [0,T] y = y_0 * e^{at}

[T,2T]: y = y_0 * (e^{at} - a*(t-T)*e^(t-T))

[2T,3T]: y = y_0 * (e^{at} - a*(t-T)e^(t-T) + 1/2 * a^2 (t-2T)^2e^(t-2T))

[3T,4T]: y = y_0 * (e^{at} - a*(t-T)e^(t-T) + 1/2 * a^2 (t-2T)^2e^(t-2T) - 1/3! * a^3 (t-3T)^3*e^(t-3T))

and then so on for each next time interval, which, up until 110, should be up until aroundd 8T. But you can see how the equations get super long the further you go. This is why I was trying to write a few lines of code, but I don't believe I'm doing that correctly even though the code runs lol.

Edit 2: Adding the code I wrote for each separate time interval

for i = 1:91
    if i <= T
        y(i,1) = exp(alpha*T);
    elseif i > T && i <= 2*T
        y(i,1) = exp(alpha*T) - alpha*exp(alpha*(i-T))*(i-T);
    elseif i > 2*T && i <= 3*T
        y(i,1) = exp(alpha*T) - alpha*exp(alpha*(i-T))*(i-T) + (1/2)*alpha^2*exp(alpha*(i-2*T))*(i-2*T)^2;
    elseif i > 3*T && i <= 4*T
        y(i,1) = exp(alpha*T) - alpha*exp(alpha*(i-T))*(i-T);
        y(i,1) = y(i,1) + (1/2)*alpha^2*exp(alpha*(i-2*T))*(i-2*T)^2;
        y(i,1) = y(i,1) - (1/6)*alpha^3*exp(alpha*(i-3*T))*(i-3*T)^3;
    elseif i > 4*T && i <= 5*T
        y(i,1) = exp(alpha*T) - alpha*exp(alpha*(i-T))*(i-T);
        y(i,1) = y(i,1) + (1/2)*alpha^2*exp(alpha*(i-2*T))*(i-2*T)^2;
        y(i,1) = y(i,1) - (1/6)*alpha^3*exp(alpha*(i-3*T))*(i-3*T)^3;
        y(i,1) = y(i,1) + (1/24)*alpha^4*exp(alpha*(i-4*T))*(i-4*T)^4;
    elseif i > 5*T && i <= 6*T
        y(i,1) = exp(alpha*T) - alpha*exp(alpha*(i-T))*(i-T);
        y(i,1) = y(i,1) + (1/2)*alpha^2*exp(alpha*(i-2*T))*(i-2*T)^2;
        y(i,1) = y(i,1) - (1/6)*alpha^3*exp(alpha*(i-3*T))*(i-3*T)^3;
        y(i,1) = y(i,1) + (1/24)*alpha^4*exp(alpha*(i-4*T))*(i-4*T)^4;
        y(i,1) = y(i,1) - (1/120)*alpha^5*exp(alpha*(i-5*T))*(i-5*T)^5;
    elseif i > 6*T && i <= 7*T
        y(i,1) = exp(alpha*T) - alpha*exp(alpha*(i-T))*(i-T);
        y(i,1) = y(i,1) + (1/2)*alpha^2*exp(alpha*(i-2*T))*(i-2*T)^2;
        y(i,1) = y(i,1) - (1/6)*alpha^3*exp(alpha*(i-3*T))*(i-3*T)^3;
        y(i,1) = y(i,1) + (1/24)*alpha^4*exp(alpha*(i-4*T))*(i-4*T)^4;
        y(i,1) = y(i,1) - (1/120)*alpha^5*exp(alpha*(i-5*T))*(i-5*T)^5;
        y(i,1) = y(i,1) + (1/720)*alpha^6*exp(alpha*(i-6*T))*(i-6*T)^6;
    elseif i > 7*T && i <= 8*T
        y(i,1) = exp(alpha*T) - alpha*exp(alpha*(i-T))*(i-T);
        y(i,1) = y(i,1) + (1/2)*alpha^2*exp(alpha*(i-2*T))*(i-2*T)^2;
        y(i,1) = y(i,1) - (1/6)*alpha^3*exp(alpha*(i-3*T))*(i-3*T)^3;
        y(i,1) = y(i,1) + (1/24)*alpha^4*exp(alpha*(i-4*T))*(i-4*T)^4;
        y(i,1) = y(i,1) - (1/120)*alpha^5*exp(alpha*(i-5*T))*(i-5*T)^5;
        y(i,1) = y(i,1) + (1/720)*alpha^6*exp(alpha*(i-6*T))*(i-6*T)^6;
        y(i,1) = y(i,1) - (1/5040)*alpha^7*exp(alpha*(i-7*T))*(i-7*T)^7;
    elseif i > 8*T && i <= 9*T
        y(i,1) = exp(alpha*T) - alpha*exp(alpha*(i-T))*(i-T);
        y(i,1) = y(i,1) + (1/2)*alpha^2*exp(alpha*(i-2*T))*(i-2*T)^2;
        y(i,1) = y(i,1) - (1/6)*alpha^3*exp(alpha*(i-3*T))*(i-3*T)^3;
        y(i,1) = y(i,1) + (1/24)*alpha^4*exp(alpha*(i-4*T))*(i-4*T)^4;
        y(i,1) = y(i,1) - (1/120)*alpha^5*exp(alpha*(i-5*T))*(i-5*T)^5;
        y(i,1) = y(i,1) + (1/720)*alpha^6*exp(alpha*(i-6*T))*(i-6*T)^6;
        y(i,1) = y(i,1) - (1/5040)*alpha^7*exp(alpha*(i-7*T))*(i-7*T)^7;
        y(i,1) = y(i,1) + (1/(5040*8))*alpha^8*exp(alpha*(i-8*T))*(i-8*T)^8;
    end
end

Just not sure how to condense this into something smaller...



Solution 1:[1]

If I understood you correctly, you want to use a cell array to store the solutions at all intervals, then you can plot the solution at any interval you want.

Note, howerver, your code is not representing the equation correctly. There is no relation between y(t) and the previous y(t-1), they all are multiplied by y(0) according to the equation. Please add more details if my answer is not what you want.

Edit: code update according to new explanation:

a  = 0.5;
y0 = a;
y  = zeros(110,110);
for T = 1:110
    for t = 1:110
        n = floor(t/T);
        k = 0:n;
        y(t,T) = y0*sum((-a).^k./factorial(k) .* (t-k*T).^k .* exp(a*(t-k*T)));
    end
end
% Plot y against t at all T intervals
plot(1:110,y)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1