Uploaded by tat170392

(Ta 300 219 882) MCG 3340 Assignment 1 - MATLAB ODEs and integration

advertisement
Q1: ODEs
function ODE
%Set output time
%Définir le temps final
t_end = 3.0;
%Set initial condition
%Définir la condition initiale
y_0 = 0.5;
%Set number of steps
%Définir le nombre de pas
num = 512;
%Compute step size
%Calculer la taille d'un pas
dt = t_end/num;
%Storage for solution
%Stockage pour la solution
y = zeros(num+1, 1);
t = zeros(num+1, 1);
%Initial condition
%Condition initiale
t(1) = 0.0;
y(1) = y_0;
%Loop for each step
%Boucle pour chaque pas
for i = 1:num;
%Answer here
%Reponse ici
y(i+1)=y(i)+dydt(y(i),t(i))*dt;
t(i+1)=t(i)+dt;
end
%Graph (for your interest only)
%Graphique (pour votre intérêt seulement)
t_exact = linspace(0, t_end);
y1_exact = sol1(t_exact);
figure;
plot(t_exact, y1_exact, 'LineWidth', 3, 'Color', 'b')
hold on;
plot(t, y, '-o', 'Color', 'r', 'MarkerFaceColor', 'r');
xlabel('t');
ylabel('y');
legend('Exact Solution to ODE1', 'Numerical Approximation');
%Output the answer
%Afficher la r ponse
y(num+1)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function that defines the ODE
% Fonction qui définit l'EDO
function slope = dydt(y,t)
slope = -y+t;
%slope = -y^2+t;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Exact solution of ODE 1
% Solution exacte de l'EDO 1
function exact = sol1(t)
exact = 1.5*exp(-t)+t-1;
end
Table 1: Numerical solution of ODE given in Eq. (1)
Exact value
Number of steps
Approximate
solution
4
8
2.0059
2.0349
3 −3
𝑒 + 2 ≅ 2.07468060255
2
16
32
64
128
2.0541
2.0643
2.0695
2.0721
256
512
2.0734
2.0740
Table 2: Numerical solution of ODE given in Eq. (2)
Number of steps
Approximate
solution
4
8
16
32
64
128
256
512
1.5669
1.6393
1.6360
1.6342
1.6333
1.6329
1.6326
1.6325
Q2: Cartesian integration
function CartesianIntegration
%Set the limits of the surface
%Determiner les limites de la surface
x_min = -1;
x_max = 1;
y_min = -1;
y_max = 1;
%Set the number of cells in each direction
%Determiner le nombre de cellules dans chaque direction
num_x = 128;
num_y = num_x;
%Determine the values of delta X and delta Y
%Determiner les valuers de delta X et Delta Y
dx = (x_max-x_min)/num_x;
dy = (y_max-y_min)/num_y;
%Initialize the sum to 0
%Inititializer la somme a 0
sum = 0;
%Loop to go to every cell
%Boucle pour passer a chaque cellule
for i = 1:num_x
for j = 1:num_y
%Answer here
%Reponse ici
% Left Riemann
x=x_min+(i-1)*dx;
y=y_min+(j-1)*dy;
% Midpoint
% x=x_min+(i-0.5)*dx;
% y=y_min+(j-0.5)*dy;
sum=sum+equation(x,y)*dx*dy;
end
end
%Output the answer
%Afficher la r ponse
sum
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Function containing the equation
%Fonction contenant l'equation
function result = equation(x,y)
result = (x+1)^2*(y+1);
%result = exp(-(x)^2)*sin(y^2);
end
Table 3: Cartesian integration of Eq. (3)
Exact value
Grid size
Left Riemann
Midpoint
16 × 16
4.5410
5.3281
32 × 32
4.9270
5.3320
16
= 5. 3̅
3
64 × 64 128 × 128 256 × 256 512 × 512
5.1276
5.2298
5.2814
5.3073
5.3330
5.3333
5.3333
5.3333
Table 4: Cartesian integration of Eq. (4)
Grid size
Left Riemann
Midpoint
16 × 16
0.9299
0.9253
32 × 32
0.9276
0.9265
64 × 64
0.9271
0.9268
128 × 128 256 × 256 512 × 512
0.9269
0.9269
0.9269
0.9268
0.9269
0.9269
Q3: Polar integration
function PolarIntegration
%Set the max radius
%Determiner le rayon max
R_max = 1;
%Set the number of cells radially
%Determiner le nombre de cellules radial
num_R = 512;
%Set the number of cells in Theta
%Determiner le nombre de cellules en Theta
num_Theta = num_R;
%Compute dR and dTheta
%Calculer dR et dTheta
dR = R_max/num_R;
dTheta = 2*pi/num_Theta;
%Initialize the sum to 0
%Inititializer la somme a 0
sum = 0;
%Loop to go to every cell
%Boucle pour passer a chaque cellule
for i = 1:num_Theta
for j = 1:num_R
%Answer here
%Reponse ici
%LHS Riemann Sum
R=(i-1)*dR;
Theta=(j-1)*dTheta;
%Midpoint
% R=(i-0.5)*dR;
% Theta=(j-0.5)*dTheta;
x=R*cos(Theta);
y=R*sin(Theta);
sum=sum+equation(x,y)*R*dR*dTheta;
end
end
%Output the answer
%Afficher la r ponse
sum
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Function containing the equation
%Fonction contenant l'equation
function result = equation(x,y)
result = (x+1)^2*(y+1);
%result = exp(-(x)^2)*sin(y^2);
end
Table 5: Polar integration of Eq. (5)
Exact value
Grid size
Left Riemann
Midpoint
16 × 16
3.6355
3.9255
32 × 32
3.7805
3.9266
5𝜋
≅ 3.92699081699
4
64 × 64 128 × 128 256 × 256 512 × 512
3.8536
3.8902
3.9086
3.9178
3.9269
3.9270
3.9270
3.9270
Table 6: Polar integration of Eq. (6)
Grid size
Left Riemann
Midpoint
16 × 16
0.5708
0.6384
32 × 32
0.6046
0.6390
64 × 64
0.6218
0.6391
128 × 128 256 × 256 512 × 512
0.6305
0.6348
0.6370
0.6392
0.6392
0.6392
Download