Matlab: 1D Numerical PDEs without Loops - matlab

So, currently I am trying to find the numerical solutions for the berger equation, $u_t+u∗u_x=0$. The numerical solution to this equation is:
$u^{n+1}_j=u_n^j−\frac{Δx}{Δt}u^n_j(u^n_j−u^n_{j−1})$
I wrote code on Matlab that computes this. As you can see, there is only one for-loop in the code. However, my u matrix extremely large, the algorithm becomes slow. Is there a way for me to use no loops at all. I was thinking about using the command cumsum, but I am not sure how to integrate that to my program. Please look at my code. Thanks.
dx = 0.9;
dt = 0.9;
tf = 10;
l = 10;
xstep = [-1:dx:l];
tstep = [0:dt:tf];
uinit = zeros(length(tstep),length(xstep));
%%Setting Initial and Boundary Conditions
bc.left = #(t) 2;
bc.right = #(t) -1;
ic = #(x) ...
2*(x<=0) ...
-1*(x>0);
uinit(1, :) = ic(xstep);
uinit(:, 1) = bc.left(tstep);
uinit(:, end) = bc.right(tstep);
%% Numerical method part
for c=1:(length(tstep)-1)
uinit(2:end -1, c+1) = uinit(2:end -1, c) - dx/dt*(uinit(2:end -1, c).*(uinit(2:end-1, c) - uinit(1:end-2, c)));
end
surf(xstep,tstep,uinit)

Related

Simplifying the solution of differential equations in Matlab

I tried various methods to simplify the solution to the following differential equation, but was unable to fully simplify it to 0.01e^(-0.15t)sin(9.999t+1.556) and expressions with radicals were not properly simplified either. Can someone please explain how the solution can be fully simplified with the number of terms reduced as much as possible?
syms y(t) m k x c
Dy = diff(y,t);
Dy2 = diff(y,t,2);
m = 10; c = 3; k = 1000;
ode = m*Dy2 +c*Dy + k*y == 0;
eqns = [ode]
cond = [y(0) == 0.01,Dy(0) == 0];
ySol(t) = dsolve(eqns,cond)
ySol(t) = simplify(ySol(t),'steps',500)
pretty(ySol(t))
vpa(ySol(t), 5)
simplify(ySol(t))
Pass output of vpa function into simplify function, as follows:
vpa(simplify(vpa(ySol(t), 3)),3)

Why my genetic algorithm doesn't converge?

At first I create a script giving the name multi_002
After I create the function that includes the equation
The script calls the function and reads the array 'a'.
Each array include one equation a(i) = x - i
I want to minimize its equation of the 4 lines array 'a'.
I suspect that something doesn't work right. I notice that
Optimtool works but f1,f2,f3,f4 doesn't go to zero. With another words
there isn't convergence.
FitnessFunction = #array_002;
numberOfVariables = 1;
[x,fval] = gamultiobj(FitnessFunction,numberOfVariables);
function [a,y,c]= array_002(x)
size = 4; n = 4;
y = zeros(size,1);
c = zeros(size,1);
for i = 1:n
y(i) = x;
c(i) = i;
a = y - c;
end
Why my genetic algorithm doesn't converge? Is there any idea?
Thank you in advance!

Solve differential equation with infinite boundary condition in Matlab

I would like to solve the following equation :
f''+tau(x)*f+f^3=0
f'(0)=0
f(inf)=1
Where tau(x) can be any function of x. The problem is to translate the boundary condition f(inf)=1 into matlab. My first solution, by reading different posts on the web was to approximate infinity by a large finite number but it doesn't give satisfying solution. Here is the code that I use :
function f = solve_GL(tau)
options = bvpset('RelTol', 1e-5);
Xstart = 0;
Xend = 1000;
solinit = bvpinit(linspace(Xstart, Xend, 50), [0, 1]);
sol = bvp4c(#twoode, #twobc, solinit, options);
x = linspace(Xstart,Xend);
y = deval(sol,x);
plot(x,y(1,:))
function dydx = twoode(x,y)
dydx = [y(2); (-heaviside(x-2)+1).*y(1)+y(1).^3];
function res = twobc(ya,yb)
res = [ya(2); yb(1)-1];
If somebody has a solution to this problem, I would be glad to hear it !

Trapezoidal Numerical Integration in MATLAB without using a FOR loop?

I'm following a Numerical Methods course and I made a small MATLAB script to compute integrals using the trapezoidal method. However my script uses a FOR loop and my friend told me I'm doing something wrong if I use a FOR loop in Matlab. Is there a way to convert this script to a Matlab-friendly one?
%Number of points to use
N = 4;
%Integration interval
a = 0;
b = 0.5;
%Width of the integration segments
h = (b-a) / N;
F = exp(a);
for i = 1:N-1
F = F + 2*exp(a+i*h);
end
F = F + exp(b);
F = h/2*F
Vectorization is important speed and clarity, but so is using built-in functions whenever possible. Matlab has a built in function for trapezoidal numerical integration called trapz. Here is an example.
x = 0:.125:.5
y = exp(x)
F = trapz(x,y)
It is recommended to vectorize your code.
%Number of points to use
N = 4;
%Integration interval
a = 0;
b = 0.5;
%Width of the integration segments
h = (b-a) / N;
x = 1:1:N-1;
F = h/2*(exp(a) + sum(2*exp(a+x*h)) + exp(b));
However, I've read that Matlab is no longer slow at for loops.

change a m-file matlab into mathematica

I've written a matlab m-file to draw a double integral as below. Does everybody can show me its equivalent in mathematica???
tetha = pi/4;
lamb = -1;
h = 4;
tetha0 = 0;
syms x y l
n = [h.*((cos(tetha)).^2)./sin(tetha); h.*abs(cos(tetha)); 0];
ft = ((tetha - pi/2)./sin(tetha)).^4;
Rt = [cos(tetha) -sin(tetha); sin(tetha) cos(tetha)];
zt = [cos(tetha0) -sin(tetha0); sin(tetha0) cos(tetha0)];
lt = [x;y];
integrand = #(x,y)(ft.*h.*((abs(cos(tetha)).* (x.*cos(tetha)-y.*sin(tetha)))-((cos(tetha)).^2/sin(tetha)).*(x.*sin(tetha)+y.*cos(tetha))));
PhiHat = #(a,b)(dblquad(integrand,0,a,0,b));
ezsurfc(PhiHat,[0,5,0,5])
Here you go (only minimal changes made), but you'll have to do your homework to understand function definitions, integration, plotting etc. in Mathematica. Also, this is not idiomatic Mathematica, but let's not go there...
tetha=Pi/4;
lamb=-1;
h=4;
tetha0=0;
n={h*((Cos[tetha])^2)/Sin[tetha],h*Abs[Cos[tetha]],0};
ft=((tetha-Pi/2)/Sin[tetha])^4;
Rt={{Cos[tetha], -Sin[tetha]}, {Sin[tetha], Cos[tetha]}};
zt={{Cos[tetha0], -Sin[tetha0]}, {Sin[tetha0], Cos[tetha0]}};
integrand[x_,y_]:= (ft*h*((Abs[Cos[tetha]]*(x*Cos[tetha]-y*Sin[tetha]))-((Cos[tetha])^2/Sin[tetha])*(x*Sin[tetha]+y*Cos[tetha])));
PhiHat[a_,b_]:=NIntegrate[integrand[x,y],{x,0,a},{y,0,b}];
Plot3D[PhiHat[x,y],{x,0,5},{y,0,5}]