Issues with odeFunction and ode15s
Show older comments
I've been making some code to solve a system of N simultanious equations, I've made a function that creates a vector with each ode as elements, but I cannot input this vector into ode15s. I know it is because the variables are all in the form y_1 , y_2 , ..... , y_N, instead of y_1(t), .... I have tried to use odeFunction to deal with this, however it still doesn't seem to work. Any solutions would be really appreciated!!
clc;
clear;
close all;
N = 10;
initial = 1:N;
dcdt = BeckerDoringFunction(N);
eqs = dcdt;
vars = sym('y_%d',[1 N]);
f = odeFunction(eqs,vars);
[t,y] = ode15s(f,[0 5],initial);
hold on
for r = 1:N
plot(t,y(:,r))
end
hold off
I'll paste the function to make the vector of ode's as well(be warned: it's a mess, I'm somewhat new to MATLAB, but does seem to work).
function [dcdt] = BeckerDoringFunction(N)
a = zeros(N,1);
b = zeros(N,1);
for n = 1:N
a(n) = 2*n;
end
C1 = 1;
yi = sym('y_%d',[1 N]);
% Makes the a_r-1 c_1 c_r-1 terms %
for i=1:N
yi(i) = a(i)*C1*yi(i);
end
yf = circshift(sym('y_%d',[1 N]),-2);
yf(N)=0;
% Makes the a_r c_1 c_r terms %
ym = circshift(sym('y_%d',[1 N]),-1);
for n = 2:N
a(n-1) = 2*n;
end
for i=1:N
ym(i) = a(i)*C1*ym(i);
end
for n = 1:N+1
b(n) = 0.2*(n+1);
end
% Makes the b_r+1 c_r+1 terms %
for i=1:N
yf(i) = b(i+1)*yf(i);
end
% Makes the b_r c_r terms %
yb = circshift(sym('y_%d',[1 N]),-1);
for i=1:N
yb(i) = b(i)*yb(i);
end
% Makes sure the vector is the right dimensions and truncates %
yf(N-1)=0;
y = yi -yb -ym + yf;
y(N)=[];
dcdt = y;
1 Comment
Keyur Mistry
on 24 Sep 2020
Please provide the set of equations you want to implement here.
Answers (0)
Categories
Find more on Ordinary Differential Equations 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!