How Do You Solve a Differential Equation in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter ngheo1128
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
ngheo1128
Messages
1
Reaction score
0
How to do you use MATLAB to solve this?
dh/dt = 0.079577-0.066169*squareroot(h)
where ho = 1.75 and hf = 1.46 and to=0
 
Physics news on Phys.org
Welcome to PF;
There are a number of numerical methods.

If we write D as the operator for d/dt, then your problem has form Dh=b-ah,
You know you can write operators and constants as matrices and h as a vector... do that and you have an equation that MATLAB can handle.
 
MATLAB has several built-in ODE solvers. The instructions are here.

I think you have to write ODEs in the form
##
\frac{d\mathbf{h}}{dt} = \mathbf{g}(t,\mathbf{h})
##
where ##\mathbf{h}## is the thing you're trying to find and ##t## is the independent variable. In your case, ##\mathbf{h}## and ##\mathbf{g}## are one-dimensional, and your ODE is already in the form we want.

Define a function handle for ##\mathbf{g}## like this:

generator = @(t,h) 0.079577-0.066169*sqrt(h)

You don't have to name it "generator," but I always do. (It's a group-theory thing.)

Now call ode45 and give it a start time, stop time, and initial condition:

[times,solution] = ode45(generator,[0,1.46],1.75)

That will start at ##t=0##, stop at ##t=1.46##, and use the initial condition ##h(0) = 1.75##. It produces two columns named times and solution. times is a list of sample times at which ##h(t)## was calculated, and solution is a list of values of ##h(t)## at those times. (You also don't have to call them "times" and "solution.")