| New Reply |
MATLAB - Using solution of one ODE in another |
Share Thread | Thread Tools |
| Jul28-12, 11:13 PM | #1 |
|
|
MATLAB - Using solution of one ODE in another
Hi all,
I'm trying to use MATLAB to obtain simulations for some equations that describe a model. I'm new to MATLAB (though I've taken a course in C++ and another in Java), so I read a bit on the mathworks website on solving ODEs, and settled on ode45 The equations I'm trying to model are the following (see link): http://i.imgur.com/vWATx.jpg As can be seen, the first equation is a straightforward (I guess?) ODE, where the L function is defined, for some x, as L(x)=coth(x)-(1/x). I was not sure how to write that (d ln L/dE)^(-1), so I just had WolframAlpha evaluate it and I typed it in. Mentioned earlier, I believe that I got the code down for the first ODE: PHP Code:
sol = ode45(@firstode,tspan,E0) would do it...but that doesn't seem to work. Can y'all provide any suggestions? NOTE: The code written does not include anything re. the second equation as I've yet to solve the dilemma I'm facing. Sorry for the long post. Thanks all! MATLAB's a bit strange -- this would be nicer on C++ :( Sincerely, RedAnsar |
| Jul28-12, 11:25 PM | #2 |
|
|
Hey RedAnsar and welcome to the forums.
One suggestion I have is if you can solve a system of ODE's, then create a new function g(L) = ln(L) and then replace the d(LnL)/de with dg/de. |
| Jul29-12, 01:01 AM | #3 |
|
|
Thanks for the welcome, chiro.
I thought it would be a system of ODEs, but it seems that would be only in the case that my equations wouldn't contain any terms with t, only the variable sought after E.g.) y1' = y1 + y2 y2' = y1*y2 In my case, I do have terms with t (notice the cos(w*t) in the equations) Re. declaring that function...so, in C++, one can simply declare a variable a certain type E.g.) PHP Code:
Again, thanks and sorry for lots of (probably trivial) questions Red Ansar |
| Jul29-12, 02:08 AM | #4 |
|
|
MATLAB - Using solution of one ODE in another
The main data-type in MATLAB is a matrix and the matrix has to have values in it and not variables as far as I understand, and as far as my experience has told me.
The kind of thing you are talking about is dealt with computational math packages that use symbolic engines and these include things like Maple and Mathematica. In terms of solving this thing in MATLAB, what I would recommend is that you implement your own numeric solver routine based on Runge-Kutta and in MATLAB depending on your delta-apprimoximation (i.e. deltat to approximate dt), you will generate a vector of values for the t's with a step-size of delta t and create another vector for the function values that are evaluated with runge-kutta scheme. |
| Jul29-12, 02:23 AM | #5 |
|
|
I suppose a first step is reading more about Runge-Kutta, huh (it was never covered in my ODE class) |
| Jul29-12, 03:11 AM | #6 |
|
|
Code:
function RungeKuttaFourthOrderSingleVariable(f, a, h, n)
x(1) = 0;
y(1) = a;
for j = 1 : (n/h)
x(j+1) = (j*h);
temp1 = h * f(x(j),y(j));
temp2 = h * f(x(j) + 0.5*h, y(j) + 0.5*temp1);
temp3 = h * f(x(j) + 0.5*h, y(j) + 0.5*temp2);
temp4 = h * f(x(j) + h, y(j) + temp3);
y(j+1) = y(j) + (1/6) * (temp1 + 2*temp2 + 2*temp3 + temp4);
end
end
Also I should have asked, but forgot to: what is Le a function of? |
| Jul29-12, 12:56 PM | #7 |
|
|
Le is just the Langevin function, coth(x)-(1/x), with the variable ξe as the parameter: Le = L(ξe) = coth(ξe) - 1/(ξe) |
| Jul31-12, 05:24 PM | #8 |
|
|
Playing some more, I don't think the explicit function for RungeKutta is necessary. ode45 is a fourth-order RungeKutta method, after all.
I wrote some more code, this time including the second equation: firstode.m Code:
function Eprime = firstode(t,E) Eprime=(((0.0016).^(-1))*((coth(E)-(1./E))/((E).^(-2)-(csch(E)).^(2)))*(((10)/E)*cos(625*t)-1)); Code:
function Fprime = secondode(t,F) Fprime = (((0.0016).^(-1))-(1/(2*0.0016))*((1/(coth(E)-(E).^(-1)))-((E).^(-1)))*10*F*cos(625*t)); Code:
E0=10; sol_1=ode45(@firstode,[0 1],E0); t = linspace(0,1,10); E = deval(sol_1,t,1); sol_2=ode45(@secondode,[0 1],E0); F = deval(sol_2,t,1); - I get MATLAB to evaluate the first ODE. Now I have an array of numerical solutions in the interval given. So far so good. - I need to somehow plug in this array of numerical values into the second ODE, as the solution to the first ODE, the variable E, appears in the second ODE. - I want to somehow tell MATLAB: Hey, that E in the secondode M-file is the solution of the ODE you just solved. Use that. However, I get an error when running into the last two lines of the the odesolver M-file At first, I thought I could just rewrite sol_2=ode45(@secondode,[0 1],E0); into sol_2=ode45(@secondode,[0 1],E0, E); as if the values of E were some extra value I can just latch on, but that didn't work out. Any ideas...? |
| New Reply |
| Thread Tools | |
Similar Threads for: MATLAB - Using solution of one ODE in another
|
||||
| Thread | Forum | Replies | ||
| Mathematica or MATLAB script for finding Singular Solution to ODE | Calculus & Beyond Homework | 0 | ||
| determining particular solution with matlab | Engineering, Comp Sci, & Technology Homework | 0 | ||
| Can you get this solution in Matlab? | Calculus | 0 | ||
| Problems in finding analytical solution in Matlab | Math & Science Software | 2 | ||
| Schwarzschild Solution in MatLab | Special & General Relativity | 0 | ||