Project

Solving 1-D heat convection equation in MATLAB

REPORT:

The code starts with creating a vector of the values of n. Then a for loop is created for n so that the code runs for each value of n and the plot is shown. The default values are set in the start. A ones vector is used to set the default velocity of the object. Using linspace command, the distribution of x is done and dx is obtained from that vector. The value of start and stop is obtained by determining the value which has the least absolute difference and the index is noted as start and stop respectively. Then the velocity from start to stop is set as 2 m/s. Then a copy of the set vector is made. Number of time steps are obtained and the time vector is obtained using linspace. Then the main equation is inserted and explicit method is followed by updating the vector after the operation is performed. Subplot comand is used to show all 4 plots.

The plot where n=20 has a abrupt transisiton across x and the velocity drains quickly.

The plot where n= 40 has relatively smooth transisition across x and velocity reduces slowly.

The plot where n=80 has very smooth transisition across x and the velocity loss is very less.

The plot where n=160 has abrupt lines forming in the plot due to Gibb's phenomenon.

The images of plot, screenshot and code are inserted.

CODE:

n=[20 40 80 160];
for i=1:length(n)
L=1; C=1; dt=0.01; %default values
u=ones(1,n(i)); %creating vector
u(1)=1; %boundary condition
x=linspace(0,L,n(i)); %displacement vector
dx=x(2)-x(1); 

%identifying start and stop
    start=abs(x-0.1);
        [~,n_start]=min(start);
    stop=abs(x-0.3);
        [~, n_stop]=min(stop);
u(n_start:n_stop)=2; %square wave
uold=u;
uinitial=u;
nt=0.4/dt;%number of steps=total time/time step=0.4/0.01
t=linspace(0,0.4,nt);

%time step
for j=1:nt
    for k=2:n(i)
        u(k)=uold(k) - (C*dt/dx)*(uold(k)-uold(k-1));
    end
    uold=u;
    subplot(2,2,i)
    plot(x,u,'b');
    axis([0 1 0 2])
    pause(0.03)
end
end

IMAGES:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.