change initial condition with binary data in matlab

1 view (last 30 days)
clear all;
close all;
% Constants
M=315; % space domain length (in m)
c=2*pi; % wvae speed constant (300 m/s = 2*pi rad/s)
dx=5.; % grid space interval
nx=round(M/dx)+1; % number of grid in space domain
nk = nx-1; % number of waves in frequency domain
dt = 0.001666; % time step interval
nt=500; % number of time steps
tmax = dt*nt; % maximum time to calculate solution
% Initialization
u(:,1) = zeros(nx,1);
for ix=11:22
xx=(ix-1)*dx;
u(ix,1)=100*(sin(pi*((xx-50)/60)));
end
m = mean(u(:,1)); % k=0 contain the mean of the data, save it for latter
% Second initial condition
uk(:,1) = fft(u(:,1)); % Fourier Transform
uk(:,2) = complex(zeros(nx,1)); % get the Fourier coeff. for next step
for k = 1:nk,
if (k < fix((nk/2))+1),
uk(k+1,2) = (1 - dt*c*1i*(k))*uk(k+1,1);
else
uk(k+1,2) = (1 - dt*c*1i*(-nk-1+k))*uk(k+1,1);
end
end
ut = ifft(uk(:,2)); % Inverse Fourier to get u in space domain
u(:,2) = real(ut)+m; % Add the average to the real part to get u at second step
clear ut;
% Solution using spectral method for all other t
uk(:,3:nt) = complex(zeros(nx,nt-2)); % get the Fourier coeff. for next step
for it = 3:nt,
for k = 1:nk,
if (k < fix((nk/2))+1),
uk(k+1,it) = uk(k+1,it-2) - 2*dt*c*1i*(k)*uk(k+1,it-1);
else
uk(k+1,it) = uk(k+1,it-2) - 2*dt*c*1i*(-nk-1+k)*uk(k+1,it-1);
end
end
ut= ifft(uk(:,it)); % Inverse Fourier to get u in space domain
u(:,it) = real(ut)+m; % Add the average to the real part to get u at the next step
clear ut;
end
% Plotting output
xi =([1:nx])*dx; % x-axis
%x = [-pi+2*pi/nx:2*pi/nx:pi]'; % if wanted, x axis can be changed to -pi < x < pi
plot(xi,u(:,1),'-b','linewidth',2); % Plot the initial condition
hold on;
for it=10:20:nt
plot(xi,u(:,it),'-r'); % Plot the rest of the solution
end
hold off;
grid on;
axis([0 max(xi) -10 110]);
xlabel('X (m)','fontweight','bold');
ylabel('Amplitude','fontweight','bold');
title(['1D Linear Advection for t = ' num2str(tmax) ' seconds'],...
'fontsize',14,'fontweight','bold');

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 10 Nov 2019
Edited: KALYAN ACHARJYA on 10 Nov 2019
"change initial condition with binary data in matlab"
Is this?
Change here
u(:,1)=randi([0 1],nx,1)
And
uk(:,2) =randi([0 1],nx,1)
  2 Comments
iyya pangesti
iyya pangesti on 10 Nov 2019
my teacher said that i have to add something like this :
%Read data
fid=fopen('file.dat','rb');
but i don't know where i can put it on my script. and which script that i have to edit.
i am beginner in matlab.
thank you anyway mr. Acharjya
KALYAN ACHARJYA
KALYAN ACHARJYA on 10 Nov 2019
I answered based on initialization comment in the code. May be you have to load "u" data from that file.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!