arraysfunctionmatlabsymbolic-mathfunction-handle

Replicate a Time Dependent Function in MATLAB


I'm trying to replicate the following function in MATLAB: enter image description here

where beta, gamma, and delta are constants.

What I have so far is below. The summation should only be applied to y(t-i) as indicated by the square brackets in:

beta = 0.3;
gamma = 0.5;
delta = 0.2;
m = 10;
N = 100;

y_out = NARMA(beta,delta,gamma,m,N);

function y_out = NARMA(beta,delta,gamma,m,N)
t = 2:N;
y = zeros (N,1);
u = @(t) cos(beta * t);
y = y(t-1) + cumsum(gamma * u(t - m) .* u(t) + delta);
y(m+1:end) = y(m+1:end) - y(1:end-m);
y_out = y;
end

After running this code y_out is a 99 by 99 double instead of a 99 by 1 double as it should be. How do I make y_out, aka my function output y(t), a 99 by 1 matrix output? Is there a better way to go about this?

An update: My new attempt is shown below. The summation should only be applied to y(t-i) as indicated by the square brackets in:enter image description here

close all;
clear all;
clc;

beta = 0.3;
delta = 0.8;
gamma = 0.4;
m = 10

NARMA = @(y_in,t) [y_in + delta + gamma*cos(beta*(t- 
m))*cos(beta*t)+cumsum(y_in(m+1:end) - y_in(1:end-m)),t];

N = 100;
y_out = zeros(N,1);

y_out(1) = NARMA(0,0);
for t=2:N
    y_out(t) = NARMA(y_out(t-1),t-1);
end

My result for y_out is a 100 by 1 matrix with values of 1,2,3,... This is not what I should be getting. I'm not sure what's wrong here.


Solution

  • Just start simple. Type in each computation you see. Use loops, don't worry about efficiency. cumsum and the like can come later if your first attempt is not fast enough.

    When I translate the equation into code in this way, I get this:

    beta = 0.3;
    delta = 0.8;
    gamma = 0.4;
    m = 10;
    N = 100;
    
    y = zeros(N,1);
    % y(0)=0, and also y(-1)=0 etc.
    t = 1;
    y(t) = gamma * cos(beta * (t-m)) * cos(beta * t) + delta;
    for t = 2:N
        v = y(t-1);
        for i = 1:m
            if t-i > 0  % else we add 0
                v = v + y(t-i);
            end
        end
        v = v + gamma * cos(beta * (t-m)) * cos(beta * t) + delta;
        y(t) = v;
    end
    

    That's it. A loop to compute each t, and a loop for the summation. Yes, we can replace that inner loop with a call to sum. Yes, we can remove that if by properly setting the loop limits. Do we need to? No, not in the first version of the code. Keep it simple. If this is fast enough, then you're done. If it's not, then you have a version of the code to compare results against when you're trying to make it more efficient.