I am newbie MATLAB user. How to write this equation system in MATLAB?

$$\frac{dN_{ik}}{dt}=\sum_{j=i}^{n}\sum_{l=1}^{n}\beta_{jl}b_{ikjl}S_{jl}N_{jl} - S_{ik}N_{ik}$$

Here, I want to know if I take $n=3$ or any constant number what kind of equation system will I have?

How can i run this loop without entering $\beta$, $b$ and $S$ functions. I just want to see matrix form of this system. Sorry for my bad English, thanks for help.


The function for the right-hand side and arbitrary parameters should look something like this:

function dNdt = rhs(t,N,par)

dNdt = zeros(length(N),1);
nk = length(N)/par.n;

for i = 1 : par.n
    for k = 1 : nk
        for j = i : par.n
            for l = 1 : par.n
                dNdt((i-1)*nk + k) = dNdt((i-1)*nk + k) + beta(j,l) * b(i,k,j,l) * S(j,k) * N((j-1)*nk + k) - S(i,k) * N((i-1)*nk + k);

where you need to provide the parameter $n$ and the implementations of the functions $\beta, b$ and $S$.

To have it run in Matlab, type

[t,N] = ode45(@(t,N)rhs(t,N,par), [t0, te], N0);

where the first argument is an anonymous function allowing for passing over a third argument (the parameter struct par), $t_0$ is start time, $t_e$ is end time and $N_0$ are initial conditions. Note that $N_0$ needs to have $n \cdot n_k$ components.

Choosing $n = n_k = S = \beta = N_0 = 1$ and $b = 0.5$ yields the following result for $t \in [0, 1]$:

enter image description here

Choosing $n = n_k = 2$, $S = \beta = 1$, $N_0 = (.2, .3, .4, .5)^T$ and $b = 0.5$ returns

enter image description here

  • $\begingroup$ Thank you for your help dear @max-herrmann, but although provide required parameters and functions, I'm getting an error like this: Undefined function or variable "dNdt". Error in rhs (line 7) $\endgroup$ – Candan Oct 5 '16 at 9:23
  • $\begingroup$ The name (or identifier) of the function is rhs, the return value is dNdt. Maybe you put it the other way around. Btw, I just added the mandatory independent variable t. $\endgroup$ – Max Herrmann Oct 5 '16 at 9:30
  • $\begingroup$ Why would you use for loops in matlab? At least last time i used it it was extremely slow. $\endgroup$ – mathreadler Oct 5 '16 at 9:54
  • $\begingroup$ For educational reasons. Easier to read. Apart from that, I didn't know for loops were `extremely slow'.. @candancelik: Corrected an index flaw. $\endgroup$ – Max Herrmann Oct 5 '16 at 9:55
  • $\begingroup$ Well, thank you! But, I can't run. Did you try this code with your arbitrary variables $t_{0}$ and $t_{e}$? @max-herrmann $\endgroup$ – Candan Oct 5 '16 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.