MATLAB How Do You Solve a Differential Equation in MATLAB?

  • Thread starter Thread starter ngheo1128
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
To solve the differential equation dh/dt = 0.079577 - 0.066169*sqrt(h) in MATLAB, first express it in the form dH/dt = g(t, H), where H is the variable to find. Define a function handle for g, such as generator = @(t,h) 0.079577 - 0.066169*sqrt(h). Use the ode45 function to compute the solution by specifying the time range and initial condition: [times, solution] = ode45(generator, [0, 1.46], 1.75). This will yield two outputs: times, which lists the sample times, and solution, which contains the corresponding values of h(t). This method effectively utilizes MATLAB's built-in ODE solvers for numerical solutions.
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.
 
You could write a method from scratch, you have two boundary conditions but only 1 constant to play with.
 
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.")
 

Similar threads

Replies
5
Views
3K
Replies
5
Views
3K
Replies
3
Views
5K
Replies
5
Views
2K
Replies
23
Views
5K
Replies
2
Views
1K
Replies
1
Views
1K
Back
Top