Solve a second-order differential equation with constant parameters changing through an external parameter

10 views (last 30 days)
Hello everyone,
I know that this a very vague question, but I am quite lost in this aspect, so I am asking for some help to begin to try to solve a second order differential equation. My second-order differential equation has the following functional form: d^2 phi (t)/dx^2+OmegaR^2/4*sin(4*phi(t))-2*Omegae*gamma*(Hy*cos(phi(t)-Hx*sin(phi(t)))+2*Omegae*alpha*dphi(t)/dt. I know which are my initial conditions, phi(t=0)=0, dphi(t)/dt (t=0)=0. Even I know which is the value of my coefficients OmegaR, Omegae, gamma, Hy, Hx and alpha for each value of an external temperature, let's say, temperature, Temperature variable, not involved in the second-order differential equation. So at the end, I want to solve that second-order differential equation in a certain time (t) interval, and plot it. It would be great if in a future I can do implement all the solutions for all the temperatures in a single plot indicating in a legend for which temperature each line corresponds, but that it is other stage of the problem. I have read the documentation of MatLab, and some posts, but I am quite lost, certainly. Someone can give an example and a strategy from which I could begin?
Also I would want to give a time-dependence to H_x and H_y, like being linear in a certain time interval H_x=k*t, given a k value, and after reaching a certain value, to be constant the rest of the time imposed in the process of solving the second-order differential equation.
Thank for your attention.
Edit: Here I present my approach until now
First, I create a function file, with all the parameters defined for a certain value of the external parameter temperature (Temperature), equal to 0. First, I am not sure if I have to upload the values in my function file (I mean, OmegaR, Omegae, gamma, Hy, Hx and alpha, because I the generalized case I will have to load a file which will contain these values for different value of temperature, which I have to remember that it is distinct to phi and time (t)):
function dx=fx(t,x);
% Global variables: Hx, Hy, OmegaR, Omegae, gamma, alpha
% Here, x(1)=phi, x(2)=dot{phi}
dx=zeros(2,1);
dx(1)=x(2);
mu0=4*pi*10^(-7); %H/m
gamma=2.21*10^5; %m/(A*s)
kB=1.38064852*10^(-23); %J/K
a0=3.328*10^(-10); %m
c=8.539*10^(-10); %m
V=c*(a0^2); %m^3
Hx=0;
Hy=100*79.5774715459; %A/m
alpha=0.001;
muB=9.27400994*10^(-24); %(T^2*m^3)/J
mu=4*muB; %(T^2*m^3)/J
Ms=mu/V; %T^2/J
Omegae=(2*gamma*abs(4*(-396*kB/V)-532*kB/V))/(mu0*Ms); %s^-1
K4parallel=1.8548*(10^(-25))/V; %J/m^3
Omega4parallel=(2*gamma*K4parallel)/(mu0*Ms); %s^-1
OmegaR=sqrt(2*Omegae*Omega4parallel);
dx(2)=-(OmegaR.^2/4).*sin(4*x(1))-2*Omegae*gamma*(Hy*cos(x(1))-Hx*sin(x(1)))+...
2*Omegae*alpha*x(2);
end
After that, I can solve my equation for a certain time interval:
clc
clear all
close all force
output='Figures';
%% First, let's write which are the initial values of our problem
% For t=0, we have that phi=x(1)=0, and that \dot{phi}=x(2)=0
xinitial=0;
dxinitial=0;
initial_conditions=[xinitial dxinitial];
%% Now, let's create the array of times on which we are interested
time=[0:0.001:40].*(10^(-12)); %s
%% Let's solve the second-order differential equation, where the X variable will have to columns
% Probably, the first column corresponds to phi=x(1) and the second one to
% \dot{phi}=x(2)
[T,X]=ode45(@fx,time,initial_conditions);
No error occurs. But I am not sure why X is an array of 40001x2 elements, what it is in each column? The first one phi and the second one its first derivative? Moreover, as I have said, this for the more simplified case of a single set of parameters for a certain value of the external parameter temperature, so I was wonder how to confront the situation in which I have multiple values for the aforementioned parameters.
Moreover, I am interested in how to implement the solution of my second-order differential equation without specifying a priori the values of Hx and Hy, because I want to take control on each of them each time that I run the program. It would be great if I run the program and MatLab ask me which value of Hx and Hy I want to use.

Accepted Answer

Star Strider
Star Strider on 24 Feb 2020
It appears that what you want to do is to pass your data to your ODE function ‘fx’. See the documentation section on Passing Extra Parameters to understand how to do that.
Please do not use global variables!
function dx=fx(t,x);
% Global variables: Hx, Hy, OmegaR, Omegae, gamma, alpha
Do this instead:
function dx = fx(t, x, Hx, Hy, OmegaR, Omegae, gamma, alpha);
and your ode45 call then becomes:
[T,X] = ode45(@(t,x)fx(t, x, Hx, Hy, OmegaR, Omegae, gamma, alpha), time, initial_conditions);
I assume that the additional variables are scalars that do not change during the integration, and that you want to run several integrations, each with different additional parameters. You can do this with a for loop, saving ‘T’ and ‘X’ as cell arrays in each iteration (easiest), then plotting them individually at the end (likely also with a loop).
  12 Comments
Roderick
Roderick on 26 Feb 2020
I understood perfeclty your approach and concerns, sorry if from my previous message that was not clear. Anyway, I have been able to implement it, based on your guidance but on my own terms. That closes the question entirely. Thank for your useful answers!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!