Issues with odeFunction and ode15s

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

Keyur Mistry
Keyur Mistry 2020년 9월 24일
Please provide the set of equations you want to implement here.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

질문:

2020년 9월 21일

댓글:

2020년 9월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by