Basic Goal: Solve & Plot the solution of an ODE
D2f=a1*f+a0
where
a1=k*H*H/c;
a0=-H*H*tau0/c;
c=0.1;
k=1;
H=1;
tau0=1;
I am using dsolve command to find the solution with the above mentioned values of constants.
sol=dsolve('D2th=a1*th+a0','th(0)=0','th(1)=0','t');
Instead of getting a solution with assigned values of constants, I get the solution as:
sol =
(exp(a1^(1/2)*t)*(a0 - a0*exp(-a1^(1/2))))/(a1*(exp(a1^(1/2)) - exp(-a1^(1/2)))) - (exp(-a1^(1/2)*t)*(a0 - a0*exp(a1^(1/2))))/(a1*(exp(a1^(1/2)) - exp(-a1^(1/2)))) - a0/a1
I need MATLAB to automatically replace the values of constants (a1, a0, etc.,) and then solve the equation, so that the results show up in complete simplified form.
Secondly, I want to plot the solution of above ODE as follows:
t0=0;
tf=1;
N=100;
h=(tf-t0)/N;
t=t0+(0:N)'*h;
plot(t,sol)
I have tried plot(t,sol(t))
but nothing works fine.
Summary of question: first solve the ODE by replacing the values of constant in solution, and then plot the solution with respect to a column vector t.
The solution is a symbolic expression. What you need is substitution in symbolic expressions and plotting of symbolic expressions. A quick search on the Matlab website gives subs
and ezplot
as suitable functions. I fixed the code but please check the working of those two functions either on the web or in your Matlab.
H=1;
k=1;
c=0.1;
tau0=1;
a1=k*H*H/c;
a0=-H*H*tau0/c;
sol=dsolve('D2th=a1*th+a0','th(0)=0','th(1)=0','t');
sol2 = subs(sol, [sym('a0'),sym('a1')], [a0, a1]);
t0=0;
tf=1;
ezplot(sol2, [t0, tf]);