Index exceeds the number of array elements. Index must not exceed 1.

8 views (last 30 days)
function res = digestive_model()
syms x(y)
c = 7; tau = 1; K = 1; c0 = 1; c1 = 1; a = 1; b = 1; v0 = 0.001;
Wt = linspace(0,100); Vt = linspace(0,100);
dx = diff(x);
at = tau*(1-(1/c)*diff(x,y,1));
ode = diff(x,y,2) + (K./Wt).*diff(x,y,1) - at.*((c0+c1.*Vt)./(a+b*x)) == 0;
cond1 = x(0) == 0;
cond2 = dx(0) == v0;
[V] = odeToVectorField(ode);
M = matlabFunction(V,'var',{'Y','X'});
res = ode45(M,[0 20],[cond1 cond2]);
figure
fplot(res,[0 20])
end
Any help would be greatly appreciated, thank you.
  2 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 26 Nov 2021
Dont attach the code as an image. Insert it as code in the message, using the editor facility for that. If someone is to help you they will for now write off what can be seen in the image - why making it harder for us to help you?
Jack Verderber
Jack Verderber on 26 Nov 2021
My apologies, first time asking a question on here so was unsure on the best way to do it. Should be fixed now.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Nov 2021
digestive_model()
ode(y) = 
Error using mupadengine/feval_internal
Unable to convert the initial value problem to an equivalent dynamical system. Either the differential equations cannot be solved for the highest derivatives or inappropriate initial conditions were specified.

Error in odeToVectorField>mupadOdeToVectorField (line 171)
T = feval_internal(symengine,'symobj::odeToVectorField',sys,x,stringInput);

Error in odeToVectorField (line 119)
sol = mupadOdeToVectorField(varargin);

Error in solution>digestive_model (line 20)
[V] = odeToVectorField(ode);
function res = digestive_model()
syms x(y)
c = 7; tau = 1; K = 1; c0 = 1; c1 = 1; a = 1; b = 1; v0 = 0.001;
Wt = linspace(0,100); Vt = linspace(0,100);
dx = diff(x);
at = tau*(1-(1/c)*diff(x,y,1));
ode = diff(x,y,2) + (K./Wt).*diff(x,y,1) - at.*((c0+c1.*Vt)./(a+b*x)) == 0;
cond1 = x(0) == 0;
cond2 = dx(0) == v0;
ode
[V] = odeToVectorField(ode);
M = matlabFunction(V,'var',{'Y','X'});
res = ode45(M,[0 20],[cond1 cond2]);
figure
fplot(res,[0 20])
end
Notice your ode is a vector -- a vector the same length as Wt and Vt since those are both vectors. If it was going to work at all, you would need a vector of initial conditions at least as long as that vector.
Also, the first entry in Wt is 0, so K./Wt includes a division by 0, so the first entry in ode involves an infinity.
If Wt and Vt are parameters of the ode, then use them as symbolic, and use odeFunction() instead of matlabFunction and specify Wt and Vt as parameters of the system. You may need to iterate over all the Wt / Vt combinations to solve all of the cases.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!