change initial condition with binary data in matlab

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');

回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 10 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 10 日

0 投票

"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 件のコメント

iyya pangesti
iyya pangesti 2019 年 11 月 10 日
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 2019 年 11 月 10 日
I answered based on initialization comment in the code. May be you have to load "u" data from that file.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Import and Analysis についてさらに検索

質問済み:

2019 年 11 月 10 日

コメント済み:

2019 年 11 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by