MATLAB - Getting an NaN error - matlab

I'm trying to generate plots of a galactic orbit in a particular potential. My code is given by
function Eulersystem_MNmodel()
parsec = 3.08*10^18;
r_1 = 8.5*1000.0*parsec; % This converts our value of r into cm.
z_1 = 1.0;
theta_1 = 0.0; %Initial Value for Theta.
U_1 = 100.0*10^5; %Initial value for U in cm/sec
V = 156.972*10^5; %Proposed value of the radial velocity in cm/sec
W_1 = 150*10^5.0;
grav = 6.6720*10^-8; %Universal gravitational constant
amsun = 1.989*10^33; %Proposed Angular momentum of the sun
amg = 1.5d11*amsun;
gm = grav*amg; %Note this is constant
nsteps = 50000; %The number of steps
deltat = 5.0*10^11; %Measured in seconds
angmom = r_1*V; %The angular momentum
angmom2 = angmom^2.0; %The square of the angular momentum
E = -gm/r_1 + U_1*U_1/2 + angmom2/(2.0*r_1*r_1); %Total Energy of the system
time = 0.0;
for i=1:nsteps
r_1 = r_1 + deltat*U_1;
U_1 = U_1 + deltat*((-gm*r_1)/((r_1^2.0 + (1+sqrt(z_1^2.0+1))^2.0)^1.50))
z_1 = z_1 + deltat*W_1;
W_1 = W_1 + deltat*(gm*z_1*(1+sqrt(z_1^2.0+1))/(sqrt(z_1^2.0+1))*(r_1^2.0+(1+sqrt(z_1^2.0+1))^2.0)^1.5);
E = -gm/r_1+U_1/2.0+angmom2/(2.0*r_1*r_1);
ecc = (1.0 + (2.0*E*angmom2)/(gm^2.0))^0.5;
time1(i) = time;
time = time+deltat;
r(i) = r_1;
z(i) = z_1;
end
figure()
plot(r,z)
I keep getting straight lines, rather than a more interesting curve, which led me to investigate the output of the U_1 function. Upon this investigation I realised that it was consistently outputting NaN "Not a Number". I can't see why I'm getting this however. I've tried replacing sqrt() with ()^0.5, this still yields NaN.

You are operating with rather large numbers. The largest floating point number is something like 1.7e+308. Lager numbers would evaluate to Inf. Similarly, positive numbers smaller than 1.7e-308 would evaluate to zero and cause NaN during division?

Related

How to perform adaptive step size using Runge-Kutta fourth order (Matlab)?

For me, it seems like the estimated hstep takes quite a long time and long iteration to converge.
I tried it with this first ODE.
Basically, you perform the difference between RK4 with stepsize of h with h/2.Please note that to reach the same timestep value, you will have to use the y value after two timestep of h/2 so that it reaches h also.
frhs=#(x,y) x.^2*y;
Is my code correct?
clear all;close all;clc
c=[]; i=1; U_saved=[]; y_array=[]; y_array_alt=[];
y_arr=1; y_arr_2=1;
frhs=#(x,y) 20*cos(x);
tol=0.001;
y_ini= 1;
y_ini_2= 1;
c=abs(y_ini-y_ini_2)
hc=1
all_y_values=[];
for m=1:500
if (c>tol || m==1)
fprintf('More')
y_arr
[Unew]=vpa(Runge_Kutta(0,y_arr,frhs,hc))
if (m>1)
y_array(m)=vpa(Unew);
y_array=y_array(logical(y_array));
end
[Unew_alt]=Runge_Kutta(0,y_arr_2,frhs,hc/2);
[Unew_alt]=vpa(Runge_Kutta(hc/2,Unew_alt,frhs,hc/2))
if (m>1)
y_array_alt(m)=vpa(Unew_alt);
y_array_alt=y_array_alt(logical(y_array_alt));
end
fprintf('More')
%y_array_alt(m)=vpa(Unew_alt);
c=vpa(abs(Unew_alt-Unew) )
hc=abs(tol/c)^0.25*hc
if (c<tol)
fprintf('Less')
y_arr=vpa(y_array(end) )
y_arr_2=vpa(y_array_alt(end) )
[Unew]=Runge_Kutta(0,y_arr,frhs,hc)
all_y_values(m)=Unew;
[Unew_alt]=Runge_Kutta(0,y_arr_2,frhs,hc/2);
[Unew_alt]=Runge_Kutta(hc/2,Unew_alt,frhs,hc/2)
c=vpa( abs(Unew_alt-Unew) )
hc=abs(tol/c)^0.2*hc
end
end
end
all_y_values
A better structure for the time loop has only one place where the time step is computed.
x_array = [x0]; y_array = [y0]; h = h_init;
x = x0; y = y0;
while x < x_end
[y_new, err] = RK4_step_w_error(x,y,rhs,h);
factor = abs(tol/err)^0.2;
if factor >= 1
y_array(end+1) = y = y_new;
x_array(end+1) = x = x+h;
end
h = factor*h;
end
For the data given in the code
rhs = #(x,y) 20*cos(x);
x0 = 0; y0 = 1; x_end = 6.5; tol = 1e-3; h_init = 1;
this gives the result against the exact solution
The computed points lie exactly on the exact solution, for the segments between them one would need to use a "dense output" interpolation. Or as a first improvement, just include the middle value from the half-step computation.
function [ y_next, err] = RK4_step_w_error(x,y,rhs,h)
y2 = RK4_step(x,y,rhs,h);
y1 = RK4_step(x,y,rhs,h/2);
y1 = RK4_step(x+h/2,y1,rhs,h/2);
y_next = y1;
err = (y2-y1)/15;
end
function y_next = RK4_step(x,y,rhs,h)
k1 = h*rhs(x,y);
k2 = h*rhs(x+h/2,y+k1);
k3 = h*rhs(x+h/2,y+k2);
k4 = h*rhs(x+h,y+k3);
y_next = y + (k1+2*k2+2*k3+k4)/6;
end
Revision 1
The error returned is the actual step error. The error that is required for the step size control however is the unit step error or error density, which is the step error with divided by h
function [ y_next, err] = RK4_step_w_error(x,y,rhs,h)
y2 = RK4_step(x,y,rhs,h);
y1 = RK4_step(x,y,rhs,h/2);
y1 = RK4_step(x+h/2,y1,rhs,h/2);
y_next = y1;
err = (y2-y1)/15/h;
end
Changing the example to a simple bi-stable model oscillating between two branches of stable equilibria
rhs = #(x,y) 3*y-y^3 + 3*cos(x);
x0 = 0; y0 = 1; x_end = 13.5; tol = 5e-3; h_init = 5e-2;
gives plots of solution, error (against an ode45 integration) and step sizes
Red crosses are the step sizes of rejected steps.
Revision 2
The error in the function values can be used as an error guidance for the extrapolation value which is of 5th order, making the method a 5th order method in extrapolation mode. As it uses the 4th order error to predict the 5th order optimal step size, a caution factor is recommended, the code changes in the appropriate places to
factor = 0.75*abs(tol/err)^0.2;
...
function [ y_next, err] = RK4_step_w_error(x,y,rhs,h)
y2 = RK4_step(x,y,rhs,h);
y1 = RK4_step(x,y,rhs,h/2);
y1 = RK4_step(x+h/2,y1,rhs,h/2);
y_next = y1+(y1-y2)/15;
err = (y1-y2)/15;
end
In the plots the step size is appropriately larger, but the error shows sharper and larger spikes, this version of the method is apparently less stable.

Triangle Pulse with fourier transformation

I have a basic exercise for telecommunications with matlab, and i must plot a triangle pulse with (-c,0) to (c,0) with c = 6 and Amplitude = 1 in a for loop for M pulses and approach the periodic pulse using N Fourier series terms. I can't find something on the internet that can help me so far.
A similar code for rect pulse that I made and works is this:
a = 1;
b = 3;
N = 1000;
t = linspace(a-2*a,b+2*b,N);
A = 1;
y = rect_pulse(A,a,b,t);
plot(t,y);
grid on
axis([a-2*a b+2*b 0 2*A]);
M = 5;
T=7;
t_new = linspace(a-2*a,b+(M-1)*T+2*b,N);
y_new = zeros(1,N);
for index = 1:1:M
temp_y = rect_pulse(A,a+(index-1)*T,b+(index-1)*T,t_new);
y_new = y_new + temp_y;
end
figure;
plot(t_new,y_new);
grid on;
axis([a-2*a b+(M-1)*T+2*b 0 2*A]);
Where rect_pulse is this:
function y = rect_pulse (A,a,b,t)
N=length(t);
y = zeros(1,N);
for index = 1:1:N
if(t(1,index)>=a) && (t(1,index)<=b)
y(1,index) = A;
end
end
And fourier series is this:
function y_fourier = fourier_series_rect_pulse(a,b,To,N,t)
y_fourier = 0;
wo = (2*pi)/To;
for n = -N:1:N
f_real = #(x) cos(n*wo*x);
f_imag = #(x) sin(n*wo*x);
cn = (1/To)*(quad(f_real,a,b)) - j*quad(f_imag,a,b));
y_fourier = y_fourier + cn*exp(j*n*wo*t);
end
y_fourier = real(y_fourier);
Any ideas how to make this in to triangle pulse?
This probably deviates significantly from your approach but if you're curious here is a script I came up with to generate a triangular pulse train that can be adjusted. This method, unfortunately, uses the fft() function which may or may not be off-limits in your case. Most of the script uses indexing and manipulating vectors. Additional spectral components may be seen due to the DC offset of the alternating triangular wave and the limited number of cycles available in the vector representation of the triangular wave.
Triangular Pulses and Fourier Transforms:
Triangular Pulse with Duty-Off Period:
Higher frequency spectral components present due to the abrupt corners that occur at the transition states of the triangle pulse and duty-off period.
%******************************************************%
%PROPERTIES THAT CAN BE CHANGED%
%******************************************************%
Plotting_Interval = 0.01; %0.01 seconds%
Pulse_Width = 1/20; %6 seconds%
Period = 1/20; %10 seconds (should be at least the pulse width)%
Start_Time = 0;
End_Time = Pulse_Width*1000; %(1000 pulses)%
%******************************************************%
if(Period < Pulse_Width)
Period = Pulse_Width;
end
Time_Vector = (Start_Time: Plotting_Interval: End_Time);
Points_Per_Unit_Time = 1/Plotting_Interval;
Half_Pulse = Pulse_Width/2;
Number_Of_Points = Pulse_Width/Plotting_Interval;
Rising_Slope = linspace(0,1,floor(Number_Of_Points/2) + 1);
Falling_Slope = 1 - Rising_Slope;
Triangular_Pulse = [Rising_Slope Falling_Slope(2:end)];
t = (0: Plotting_Interval: Pulse_Width);
Periodic_Triangular_Pulse = zeros(1,length(Time_Vector));
for Cycle = 1: +Period/Plotting_Interval: End_Time/Plotting_Interval
Periodic_Triangular_Pulse(1,Cycle:Cycle+length(Triangular_Pulse)-1) = Triangular_Pulse(1,1:end);
end
Periodic_Triangular_Pulse = Periodic_Triangular_Pulse(1,1:length(Time_Vector));
subplot(1,2,1); plot(Time_Vector,Periodic_Triangular_Pulse);
Triangle_Frequency = 1/Period;
title("Triangular Pulse Train " + num2str(Triangle_Frequency) + "Hz (first 10 cycles)");
axis([0 Period*10 0 1]);
xlabel("Time (s)"); ylabel("Amplitude");
Signal_Length = length(Periodic_Triangular_Pulse);
Fourier_Transform = fft(Periodic_Triangular_Pulse);
Fs = 1/Plotting_Interval;
P2 = abs(Fourier_Transform/Signal_Length);
P1 = P2(1:floor(Signal_Length/2)+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(Signal_Length/2))/Signal_Length;
subplot(1,2,2); plot(f,P1)
title("Single-Sided Fourier Transform");
xlabel("Frequency (Hz)"); ylabel("Magnitude");
Ran using MATLAB R2019b

Unknown error in code to find the angle necessary for a rocket to land on the moon

I need to find the angle of a rocket given the conditions in the code that enables it to land on the moon. The moon is taken as stationary at the given coordinates. I assumed the only way to try would just be calculating the different velocity components and then inputting them for each angle from the minimum to the maximum, which is what I've tried to do.
Not sure why this isn't working - it just runs forever and doesn't output anything.
Thank you.
I have
v = 0.0066;
for angle = 0:.5:180
init_vel = [v*cosd(angle), v*sind(angle)];
init_pos = [3.7,0];
t = 10;
moon_pos = [0,222];
%simulate rocket
[tout, pos] = simulaterocketmovement(init_pos, init_vel, moon_pos, t);
if(length(tout)<99999)
break;
end
end
disp(angle);
plot(pos(:,1),pos(:,2));
where
function [ tout , pos ] = simulaterocketmovement ( init_pos , init_vel , moon_pos , t)
%Defining initial variables for later use
G = 9.63*10^-7;
Me = 83.3;
%Defining the two vectors we will use
x = init_pos(1);
y = init_pos(2);
vx = init_vel(1);
vy = init_vel(2);
%Need to create a seperate function that integrates to find the
%acceleration using Euler's second order method.
function a = acc(x,y)
ax = -(G*Me*x)/((x^2 + y^2)^1.5) - (G*(x-moon_pos(1)))/(((moon_pos(1)-x)^2 + (moon_pos(2)-y)^2)^1.5);
ay = -(G*Me*y)/((x^2 + y^2)^1.5) - (G*(y-moon_pos(2)))/(((moon_pos(1)-x)^2 + (moon_pos(2)-y)^2)^1.5);
%After finding the vector components, we put them in an acceleration vector.
a = [ax,ay];
end
%Now we find the values which result in the rocket landing on the moon. The
%range of values lie between xmin and xmax, and ymin and ymax. The +/-
%represents that the rocket can land anywhere on the moon's surface.
xmax = moon_pos(1) + 1;
xmin = moon_pos(1) - 1;
ymax = moon_pos(2) + 1;
ymin = moon_pos(2) - 1;
%For each time taken, to find the x and y values we need to use a while
%loop which works until the rocket is in the range of values for it to
%land on the moon.
while((x(end) > xmax) || (x(end) < xmin) || (y(end) < ymin) || (y(end) > ymax) )
%We assign temporary new values of x and y.
x(end+1) = x(end) + t*vx;
y(end+1) = y(end) + t*vy;
%Then we find the values of acceleration for both the old and new x and y
aold = acc(x(end-1), y(end-1));
anew = acc(x(end), y(end));
%Using this to find the new velocities
vxnew = vx + t*(aold(1)+anew(1))/2;
vynew = vy + t*(aold(2)+anew(2))/2;
%Final, more accurate values for x and y
x(end) = x(end-1) + t*(vxnew + vx)/2;
y(end) = y(end-1) + t*(vynew + vy)/2;
%And updating these as the new velocities
vx = vxnew;
vy = vynew;
end
%Then we construct a vector for the time steps, for the entire journey.
tout = 0:t:((length(x)-1)*t);
%And then create a position vector which includes the x and y positions.
pos = zeros(length(x),2);
pos(:,1) = x;
pos(:,2) = y;
end

solving a simple advection equation (1D)

I am trying to solve a 1D advection equation in Matlab as described in this paper, equations (55)-(57). I am making use of the central difference in equaton (59).
I would ultimately like to get something like figure (2) in the paper, which is the result of solving the advection equation for an advection velocity e(1-k) with k=1, i.e. a stationary wave. However, my code keeps diverging. Here is what I have so far:
%initial parameters
e = 1.0;
k = 0.5;
N = 120;
lx = 120;
%initialization of sine
for x=1:lx
if(x<3*lx/4+1 && x>lx/4+1)
phi(x) = sin(2*pi*(x-1-lx/4)/lx);
else
phi(x) = 0.0;
end
end
%advection loop
for t=1:N
gradPhi = 0.5*(+circshift(phi, [0,-1]) - circshift(phi, [0,+1]));
phiBar = phi + 0.5*k*e*gradPhi;
phiOutbar = circshift(phiBar, [0,-1]);
gradPhi = 0.5*(+circshift(phiOutbar, [0,-1]) - circshift(phiOutbar, [0,+1]));
phi = phiOutbar + 0.5*k*e*gradPhi;
end
plot(phi)
Where is the error in my simple code?
You haven't included the time step dt in your update equation. The term in equation (55) says k * e * dt / 2.
This is making your update really unstable and leading to divergence. For stability, you need CFL of 1, and yours is currently around 120. Try updating your code this way:
dt = 1/120;
%advection loop
for t=1:N/dt
gradPhi = 0.5*(+circshift(phi, [0,-1]) - circshift(phi, [0,+1]));
phiBar = phi + 0.5*dt*k*e*gradPhi;
phiOutbar = circshift(phiBar, [0,-1]);
gradPhi = 0.5*(+circshift(phiOutbar, [0,-1]) - circshift(phiOutbar, [0,+1]));
phi = phiOutbar + 0.5*dt*k*e*gradPhi;
end

does white noise without mean zero do some changes?

again my question is related to white noise ,but with different meaning.let us compare following two code.first
function [ x ] = generate(N,m,A3)
f1 = 100;
f2 = 200;
T = 1./f1;
t = (0:(N*T/m):(N*T))'; %'
wn = rand(length(t),1).*2 - 1;
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;
%[pks,locs] = findpeaks(x);
plot(x)
end
using generate(3,500,10)
graph of this code is following
but let us change our code so that it makes zero mean with white noise
function [ x ] = generate1(N,m,A3)
f1 = 100;
f2 = 200;
T = 1./f1;
t = (0:(N*T/m):(N*T))'; %'
wn = rand(length(t),1).*2 - 1;
mn=wn-mean(wn);
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*mn;
%[pks,locs] = findpeaks(x);
plot(x)
end
and graph is following
if we compare these two picture,we could say that it is almost same,just some changes,so does matter if we make zero mean or not?for real analysis,like for finding
peaks and so on.thanks very much
UPDATED:
there is updated code
function [ x ] = generate1(N,m,A3)
f1 = 100;
f2 = 200;
T = 1./f1;
t = (0:(N*T/m):(N*T))'; %'
wn = randn(length(t),1); %zero mean variance 1
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;
%[pks,locs] = findpeaks(x);
plot(x)
end
and it's picture
Your initial noise is uniformly distributed between -1 & +1
Your second noise is also uniformly disributed between -1 & +1, because mean is already zero, subtracting it is meaningless
in order to obtain white noise you can use randn() function:
wn = randn(length(t),1); %zero mean variance 1
You may not observe any much difference again if your noise coefficient A3 has a much lower value compared to 20 & 30 which are the coefficients of your signal.
In order to find peaks, adding noise may not serve any purpose because noise tends to decrease the information content of signals
What is the value of mean(wm)? If it is close to zero, then no, it does not matter.
Technically, white noise has zero mean by definition.