Non linear system of differential equations on MATLAB - matlab

I'm new on MATLAB, and I was trying for fun to resolve the Friedmann equations using the Runge-Kutta algorithm.
If you don't know it, the Friedmann equations has the following form:
Friedmann equations for expanding universe
, where the curvature k is given by the values -1, 0 or 1.
Also, the state equation for the pressure p is given by:
State equation for the pressure
So, I have the Runge-Kutta algorithm:
function [t,x,y] =rk_2_1(f,g,t0,tf,x0,y0,n)
h=(tf-t0)/n;
t=t0:h:tf;
x=zeros(n+1,1); %reserva memoria para n+1 element(i)os del vect(i)or x(i)
y=zeros(n+1,1);
x(1)=x0; y(1)=y0;
for i=1:n
k1=h*f(t(i),x(i),y(i));
l1=h*g(t(i),x(i),y(i));
k2=h*f(t(i)+h/2,x(i)+k1/2,y(i)+l1/2);
l2=h*g(t(i)+h/2,x(i)+k1/2,y(i)+l1/2);
k3=h*f(t(i)+h/2,x(i)+k2/2,y(i)+l2/2);
l3=h*g(t(i)+h/2,x(i)+k2/2,y(i)+l2/2);
k4=h*f(t(i)+h,x(i)+k3,y(i)+l3);
l4=h*g(t(i)+h,x(i)+k3,y(i)+l3);
x(i+1)=x(i)+(k1+2*k2+2*k3+k4)/6;
y(i+1)=y(i)+(l1+2*l2+2*l3+l4)/6;
end
end
, but I have three problems:
I don't know how to put symbolically the density \rhoon my function.
I don't know to define the second function g=#(t,x,y), because I have a derivative of a with respect to time.
And the last one, what happens with k=sqrt(-1)? Because I need that result symbollicaly, but I want the whole result numerically.
Sorry if my questions are basic, but I don't know how to do this and I need some advice or help.
Thank u very much :)

Related

Find point on the Spline Curve in Flutter

I am having some discrete points, by using which I can plot spline curve(Syncfusion chart) in flutter. But now I have to find the point on that curve i.e. by giving values of x, I need value of y. I am stucked here and don't have any algorithm to apply for that. How did they make graph using discrete point ? There should be some algorithm which can be applied here and get those point.
Please help me out
Thanks in advance!!
I am here with a great solution to this problem, So The idea goes like if we have n points for equilibrium data, then we will assume a polynomial of order n-1(For eg. no. of points on equilibrium curve to be 3 then the polynomial should be quadratic of form y= Ax²+Bx+C). Now as we have 3 variables(A, B, C), then to solve this equation we need 3 equations in terms of A, B and C. These equations are obtained by putting the equilibrium data points, in this case 3 points so we will get 3 equations. These three equations can be solved by using Cramer's rule. After solving the equation we will get the equation of the curve.
The equation thus obtained will be more accurate and as cramer's rule can be obtained to any number of equations, then we can easily obtain polynomial equation of any order.This method is quite big and will be time taking to apply.
This will give you the curve for a given number of points

How can I find the total harmonic distortion of a nonlinear signal. Using matlab

How can I find the total harmonic distortion of a nonlinear signal. For example, Forced Van der pol Oscillator with code as shown below. I have tried the 'thd' function in matlab but I guess I'm missing somethings.
This is the equation
x''-mu(1-x^2(t))x'(t)+x(t)=Pcos(w*t)
function vdpo()
t=0:0.001:10
mu=2
x0=-2;
v0=2;
p=10; w=7;
[t,x]= ode45(#f, t, [x0,v0])
function dxdt=f(t,x)
dxdt1=x(2); dxdt2= mu(1-x(1)^2)*x(1)+p*cos(w*t);
dxdt=[dxdt1 ;dxdt2];
end
end
Try the code below, in which function f(t,x) is our ODE equations and we call function ode45 to use Runge-Kutta methods to solve it.
function [x]=vdpo()
t=0:0.001:10
mu=2
x0=-2;
v0=2;
p=10; w=7;
[t,x]= ode45(#f, t, [x0,v0])
function dxdt=f(t,x)
dxdt1=-x(2)-x(1)+(x(1)^3)/3; dxdt2=-x(1)+p*cos(w*t);
dxdt=[dxdt1 ;dxdt2];
end
end
However, it is actually a math problem rather than a programming problem. The first thing that we have to do is to transform the equations into a more convenient form by defining y=x'+((x^3)/3-x)*mu, then we have 2 First Order Ordinary Differential Equations so we could call ode45 to solve it. I looked them through at here(page2).
By calling
X=vdpo();
x=X(:,1);
thd(x)
we could get an answer like:
p.s. NOT CERTAIN about THD part.

matlab differential equation

I have the following differential equation which I'm not able to solve.
We know the following about the equation:
D(r) is a third grade polynom
D'(1)=D'(2)=0
D(2)=2D(1)
u(1)=450
u'(2)=-K * (u(2)-Te)
Where K and Te are constants.
I want to approximate the problem using a matrix and I managed to solve
the similiar equation: with the same limit conditions for u(1) and u'(2).
On this equation I approximated u' and u'' with central differences and used a finite difference method between r=1 to r=2. I then placed the results in a matrix A in matlab and the limit conditions in the vector Y in matlab and ran u=A\Y to get how the u value changes. Heres my matlab code for the equation I managed to solve:
clear
a=1;
b=2;
N=100;
h = (b-a)/N;
K=3.20;
Ti=450;
Te=20;
A = zeros(N+2);
A(1,1)=1;
A(end,end)=1/(2*h*K);
A(end,end-1)=1;
A(end,end-2)=-1/(2*h*K);
r=a+h:h:b;
%y(i)
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
A(2:end-1,2:end-1)=A(2:end-1,2:end-1)+diag(yi);
%y(i-1)
for i=1:1:length(r)-1
ymin(i)=r(i+1)*(1/(h^2))-1/(2*h);
end
A(3:end-1,2:end-2) = A(3:end-1,2:end-2)+diag(ymin);
%y(i+1)
for i=1:1:length(r)
ymax(i)=r(i)*(1/(h^2))+1/(2*h);
end
A(2:end-1,3:end)=A(2:end-1,3:end)+diag(ymax);
Y=zeros(N+2,1);
Y(1) =Ti;
Y(2)=-(Ti*(r(1)/(h^2)-(1/(2*h))));
Y(end) = Te;
r=[1,r];
u=A\Y;
plot(r,u(1:end-1));
My question is, how do I solve the first differential equation?
As TroyHaskin pointed out in comments, one can determine D up to a constant factor, and that constant factor cancels out in D'/D anyway. Put another way: we can assume that D(1)=1 (a convenient number), since D can be multiplied by any constant. Now it's easy to find the coefficients (done with Wolfram Alpha), and the polynomial turns out to be
D(r) = -2r^3+9r^2-12r+6
with derivative D'(r) = -6r^2+18r-12. (There is also a smarter way to find the polynomial by starting with D', which is quadratic with known roots.)
I would probably use this information right away, computing the coefficient k of the first derivative:
r = a+h:h:b;
k = 1+r.*(-6*r.^2+18*r-12)./(-2*r.^3+9*r.^2-12*r+6);
It seems that k is always positive on the interval [1,2], so if you want to minimize the changes to existing code, just replace r(i) by r(i)/k(i) in it.
By the way, instead of loops like
for i=1:1:length(r)
yi(i)=-r(i)*(2/(h^2));
end
one usually does simply
yi=-r*(2/(h^2));
This vectorization makes the code more compact and can benefit the performance too (not so much in your example, where solving the linear system is the bottleneck). Another benefit is that yi is properly initialized, while with your loop construction, if yi happened to already exist and have length greater than length(r), the resulting array would have extraneous entries. (This is a potential source of hard-to-track bugs.)

how to solve first order of system of PDEs in Matlab

I have a set of 4 PDEs:
du/dt + A(u) * du/dx = Q(u)
where,u is a matrix and contains:
u=[u1;u2;u3;u4]
and A is a 4*4 matrix. Q is 4*1. A and Q are function of u=[u1;u2;u3;u4].
But my questions are:
How can I solve above equation in MATLAB?
If I solved it by PDE functions of Matlab,can I convert it to a
simple function that is not used from ready functions of Matlab?
Is there any way that I calculate A and Q explicitly. I mean that in
every time step, I calculate A and Q from data of previous time step
and put new value in the equation that causes faster run of program?
PDEs require finite differences, finite elements, boundary elements, etc. You can also turn them into ODEs using transforms like Laplace, Fourier, etc. Solve those using ODE functions and then transform back. Neither one is trivial.
Your equation is a non-linear transient diffusion equation. It's a parabolic PDE.
The equation you posted has the additional difficulty of being non-linear, because both the A matrix and Q vector are functions of the independent variable q. You'll have to start by linearizing your equations. Solve for increments in u rather than u itself.
Once you've done that, discretize the du/dx term using finite differences, finite elements, or boundary elements. You should start with a weighted residual integral formulation.
You're almost done: Next to integrate w.r.t. time using the method of your choice.
It's not trivial.
Google found this: maybe it will help you.
http://www.mathworks.com/matlabcentral/fileexchange/3710-nonlinear-diffusion-toolbox

How to solve complex system of equations using matlab?

I have to analyze 802.11 saturation throughput using matlab, and here is my problem. I'm trying to solve parametric equations below (parameters are m,W,a) using solve function and i get
Warning: Explicit solution could not be found
How could I solve above equations using matlab?
I guess you were trying to find an analytical solution for tau and p using symbolic math. Unless you're really lucky with your parameters (e.g. m=1), there won't be an analytical solution.
If you're interested in numerical values for tau and p, I suggest you manually substitue p in the first equation, and then solve an equation of the form tau-bigFraction=0 using, e.g. fzero.
Here's how you'd use fzero to solve a simple equation kx=exp(-x), with k being a parameter.
k = 5; %# set a value for k
x = fzero(#(x)k*x-exp(-x),0); %# initial guess: x=0