问题
I am deriving a second order differential equation in MATLAB. I have defined a time dependent variable and then applied following derivative operations-
syms a b;
th = sym('th(t)'); %th is a time dependent variable
y = diff(a^2*cos(th)+(b/12)*sin(th));
thd = diff(th); %derivative of th wrt time
ybythd = diff(y,thd); %derivative of y wrt thd
p = diff(ybythd); %derivative of ybythd wrt time
These operations calculates the value of p
as following-
p = diff(diff((b*cos(th(t))*diff(th(t), t))/12 - a^2*sin(th(t))*diff(th(t), t), t), diff(th(t), t))
Now, I want to plot the variable p
wrt time t
. Before plotting, I substituted the value of symbols a
and b
newP = subs(p,[a,b],[2.1,9.5])
newP = diff((19*cos(th(t))*diff(th(t), t, t))/24 - (19*sin(th(t))*diff(th(t), t)^2)/24 - (441*cos(th(t))*diff(th(t), t)^2)/100 - (441*sin(th(t))*diff(th(t), t, t))/100, diff(th(t), t))
The variable th = sin(2*pi*t);
should be substituted in order to convert the above second order differential equation into a liner equation of time t
. Later on the following command can plot p
wrt time t
-
thAct = sin(2*pi*t);%The function of th
time = 0.0:0.1:5.0;
for i = 1:length(time)
temp = subs(newP,th,thAct);
pVal(t)= subs(temp,t,time(i));
end
plot(time,pVal);
But the above code does not work. Somebody please tell me how to substitute the parameters in second order differential equation.
回答1:
The following code worked for me, however I am not 100% sure if this is exactly what you want to achieve. If it is not, please update your question accordingly.
syms a;
th = sym('th(t)');
x = a*cos(th);
v = diff(x);
acc = diff(v);
accByTh = diff(acc,t);
ezplot(subs(accByTh,th,'a*cos(t)'),[-pi,pi])
来源:https://stackoverflow.com/questions/29613141/evaluate-the-second-order-differential-equation-in-matlab