# Solving a system of second order differential equations

2 visualizaciones (últimos 30 días)
Aleem Andrew el 30 de Mzo. de 2020
Comentada: Aleem Andrew el 31 de Mzo. de 2020
I get the following error message when I run the code below (although this error does not always occur; sometimes the plot is generated). I do not see why because matlabFunction (for example, in the line "M = matlabFunction(V,'vars', {'t','Y'})" (source: https://www.mathworks.com/help/symbolic/solve-differential-equation-numerically-1.html)) is a standard name.
Error message
Error using sym/matlabFunction>checkVars (line 223)
Variable names must be valid MATLAB variable names.
Error in sym/matlabFunction (line 158)
vars = checkVars(funvars,opts);
Code
syms y(t) z(t )
Dy = diff(y,t );
Dz = diff(z,t );
eqns = [diff(y,t,2) == (29.4*cos(z)+4*Dz*Dz)*sin(z)/(1+3*sin(z)*sin(z )),...
diff(z,t,2) == -3*(19.62+2*Dz*Dz*cos(z))*sin(z)/(2*(1+3*sin(z)*sin(z )))];
fun = matlabFunction(odeToVectorField(eqns),'Vars',{'t','Y '});
tspan = [0 10]; % Time interval for integration
y0 = [0 1 0 1]; % initial conditions
[t,y] = ode45(fun,tspan,y0 );
plot(t,y)
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Steven Lord el 31 de Mzo. de 2020
"Y" (without the quotes) is a valid variable name.
"Y " (again without the quotes but with the space) is not a valid variable name.
>> syms x
>> f = x.^2;
>> M1 = matlabFunction(f, 'Vars', {'x','Y'}); % Works
>> M2 = matlabFunction(f, 'Vars', {'x','Y '}); % Does not work
Error using sym/matlabFunction>checkVars (line 223)
Variable names must be valid MATLAB variable names.
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Aleem Andrew el 31 de Mzo. de 2020
Thanks for clarifying this point

Iniciar sesión para comentar.

### Categorías

Más información sobre Conversion Between Symbolic and Numeric en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by