Second order ODE with dsolve

1 view (last 30 days)
Hello All,
Trying to solve this ODE.
clear,clc
syms y(t) x2(t) x1(t) a b
dy=diff(y,t);
ode = diff(y,t,2) == a.*d) + b.*y - diff(x2,t)./10 + x1;
cond1 = y(0)==0;
cond2 = dy(0)==0;
conds=[cond1 cond2];
sol1=dsolve(ode,conds)
t=(0:.1:20);
x2(t)=20.*heaviside(t);
x1(t)=0;
out=eval(sol1);
plot(t,out)
%%%% Getting this error %%%%
Error using eval
Must be a string scalar or character vector.
Error in Homework_1 (line 14)
out=eval(sol1);

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jan 2020
ode = diff(y,t,2) == a.*d) + b.*y - diff(x2,t)./10 + x1;
You are missing a ( . Also, dy is not defined. Perhaps d) is intended to be dy ?
You have two differential functions, y and x2, but you are only sending one equation to dsolve() . You need at least one equation for each function .
Or... you could define x2 before you define ode, so that ode becomes an equation with only one differential variable.
out=eval(sol1);
eval() is not defined for symbolic expressions. It does something in new enough releases, but the something it does is almost never what you would like to have happen. Use subs() instead.
  1 Comment
Jacob Yarinsky
Jacob Yarinsky on 25 Jan 2020
Thank you for this. I tried to remove my additional comments for clarity and mistakenly erased the y from dy. Defining x2 and x1 before creating ode worked perfectly.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!