Error using eval with a differential equation

when i do this i get this error:
>> t=0:0.01:10;
>> dsolve('D3x + 6*D2x + 11*Dx + 6*x=6*square(2*pi*t),D2x(0)=0,Dx(0)=0,x(0)=0')
ans =
exp(-t)*int(3*exp(x)*square(2*pi*x), x, 0, t, 'IgnoreSpecialCases', true, 'IgnoreAnalyticConstraints', true) + exp(-3*t)*int(3*exp(3*x)*square(2*pi*x), x, 0, t, 'IgnoreSpecialCases', true, 'IgnoreAnalyticConstraints', true) + exp(-2*t)*int(-6*exp(2*x)*square(2*pi*x), x, 0, t, 'IgnoreSpecialCases', true, 'IgnoreAnalyticConstraints', true)
>> y=eval(vectorize(ans))
Error using eval
Undefined function or variable 'x'.
what's happening? (i suppose is the square function because if i substitute it for sin(t) it works right.

1 commentaire

Stephen23
Stephen23 le 19 Mai 2018
Modifié(e) : Stephen23 le 20 Mai 2018
What do you hope to obtain by evaluating the output of dsolve? eval is not the right tool to use:
Is there a reason why you are using the very outdated char input to dsolve ?

Connectez-vous pour commenter.

Réponses (2)

Never eval() a symbolic expression. Symbolic expressions are not in the MATLAB language, just in something that is close to the MATLAB language.
But more immediately you need to
syms x

5 commentaires

Stephen23
Stephen23 le 20 Mai 2018
jose luis guillan suare's "Answer" moved here:
and how i substitute syms instead of eval in my expresion, thank you.
t=0:0.01:10;
dsolve('D3x + 6*D2x + 11*Dx + 6*x=6*square(2*pi*t),D2x(0)=0,Dx(0)=0,x(0)=0')
subs(ans)
vpa(ans)
This will take some time, and will produce a useless answer that starts
[0, ...
0.99004983374916805357390597718004*numeric::int(3*exp(x)*square(2*pi*x), x == 0..1/100) + 0.97044553354850817693252835195919*numeric::int(3*exp(3*x)*square(2*pi*x), x == 0..1/100) + 0.98019867330675530222081410422531*numeric::int(-6*exp(2*x)*square(2*pi*x), x == 0..1/100), ...
0.98019867330675530222081410422531*numeric::int(3*exp(x)*square(2*pi*x), x == 0..1/50) + 0.94176453358424870953715278327115*numeric::int(3*exp(3*x)*square(2*pi*x), x == 0..1/50) + 0.96078943915232320943921069132325*numeric::int(-6*exp(2*x)*square(2*pi*x), x == 0..1/50)
Work-around:
syms x(t)
sol = dsolve('D3x + 6*D2x + 11*Dx + 6*x=6*square(2*pi*t),D2x(0)=0,Dx(0)=0,x(0)=0');
Square(t) = piecewise(t - floor(t) < 1/2, 1, -1)
square(t) = Square(t/(2*pi))
T = 0:0.01:10;
solt = vpa(subs(subs(sol), t, T));
This is rather slow. I have evidence at the moment that not all of the values can be resolved to numeric, but I do not yet know which ones.
Which release are you using by the way? I find that R2017a cannot subs() in the definition of square to the sol, but that R2018a can substitute it.
and how i plot the equation?
This is difficult to solve analytically, probably because the differentiation is not all that well defined right at the boundary conditions.
But you can get "good enough" this way:
syms t
Square(t) = piecewise(t - floor(t) < 1/2, 1, -1)
sol_plus = dsolve('D3x + 6*D2x + 11*Dx + 6*x=6*1,D2x(0)=0,Dx(0)=0,x(0)=0');
sol = Square(t) * sol_plus;
This works because solving for =6*-1 gives the negative of sol_plus.
Once you have the above you can plot with
T = 0:0.01:10;
solt = double( subs(sol, t, T) );
plot(T, solt);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by