diff --git a/HW6Problem2.m b/HW6Problem2.m index a56c1b2..85c37d8 100644 --- a/HW6Problem2.m +++ b/HW6Problem2.m @@ -7,7 +7,6 @@ a = figure(1); set(0, 'defaultAxesFontsize', 16) set(0, 'defaultTextFontsize', 16) set(0, 'defaultLineLineWidth', 2) -hold on t = [0:dt:3]'; y_n = zeros(size(t)); y_n(1) = 1; %initial condition @@ -18,7 +17,7 @@ end plot(t, y_analytical(t), 'k-', t, y_n, 'o-'); title('Analytical vs. Euler') legend('Analytical', 'Euler', 'Location', 'Northeast') -saveas(a, 'euler.png'); +saveas(a, 'euler2.png'); %Part c. b = figure(2); @@ -26,4 +25,4 @@ y_heun = heun_sol_order1(@(t,y) ode2(t,y), dt, 1, [0 3]); plot(t, y_analytical(t), 'k-', t, y_heun, 'o-'); title('Analytical vs. Heun') legend('Analytical', 'Heun', 'Location', 'Northeast') -saveas(b, 'heun.png'); +saveas(b, 'heun2.png'); diff --git a/HW6Problem2.m~ b/HW6Problem2.m~ deleted file mode 100644 index 6f374b4..0000000 --- a/HW6Problem2.m~ +++ /dev/null @@ -1,27 +0,0 @@ -%Part a. -y_analytical = @(t) exp(-t); - -%Part b. -dt = 0.01; %step size -a = figure(1); -set(0, 'defaultAxesFontsize', 16) -set(0, 'defaultTextFontsize', 16) -set(0, 'defaultLineLineWidth', 2) -hold on -t = [0:dt:3]'; -y_n = zeros(size(t)); -y_n(1) = 1; %initial condition -for i = 2:length(t) - dy = ode2(t, y_n(i-1)); - y_n(i) = y_n(i-1) + dt*dy; -end -plot(t, y_analytical(t), 'k-', t, y_n, 'o-'); -title('Analytical vs. Euler') -legend('Analytical', 'Euler', 'Location', 'Northeast') -saveas(a, 'euler.png'); - -%Part c. -b = figure(2) -y_heun = heun_sol_order1(@(t,y) ode2(t,y), 1, 1, [0 3]); -plot([0:1:3], y_heun); -plot(t, y_analytical(t), 'k-', t, y_heun, 'o-'); diff --git a/HW6Problem3.m b/HW6Problem3.m index ecd77ce..8ef85a8 100644 --- a/HW6Problem3.m +++ b/HW6Problem3.m @@ -1,23 +1,30 @@ -%Part b. -dy = ode3(0, [1;0]); -dt = [0.1 0.01 0.001]; -figure() -hold on -for k = 1:length(dt) - t = [0:dt(k):3]'; - y_n = zeros(length(t), 2); - y_n(1, :) = [1 0]; %initial condition - for i = 2:length(t) - dy = ode3(t, y_n(i-1, :)); - y_n(i, :) = y_n(i-1, :) + dt(k)*dy; - end +%Part a. +y_analytical = @(t) cos(3*t); - plot(t, cos(3*t), 'k-', t, y_n(:,1), 'o-'); +%Part b. +dt = 0.001; %step size +a = figure(1); +set(0, 'defaultAxesFontsize', 16) +set(0, 'defaultTextFontsize', 16) +set(0, 'defaultLineLineWidth', 2) +t = [0:dt:3]'; +y_n = zeros(length(t), 2); +y_n(1, :) = [1 0]; %initial condition +for i = 2:length(t) + dy = ode3(t, y_n(i-1, :)); + y_n(i, :) = y_n(i-1, :) + dt(k)*dy; end +plot(t, y_analytical(t), 'k-', t, y_n(:,1), 'o-'); +title('Analytical vs. Euler') +legend('Analytical', 'Euler', 'Location', 'Northeast') +saveas(a, 'euler3.png'); %Part c. -figure() +b = figure(2); dt = 0.001; t = [0:dt:3]; y_heun = heun_sol_order2(@(t,y) ode3(t,y), dt, [1 0], [0 3]); -plot(t, cos(3*t), 'k-', t, y_heun(:,1), 'o-'); \ No newline at end of file +plot(t, y_analytical(t), 'k-', t, y_heun(:,1), 'o-'); +title('Analytical vs. Heun') +legend('Analytical', 'Heun', 'Location', 'Northeast') +saveas(b, 'heun3.png'); \ No newline at end of file diff --git a/HW6Problem3.m~ b/HW6Problem3.m~ new file mode 100644 index 0000000..8fdfbc5 --- /dev/null +++ b/HW6Problem3.m~ @@ -0,0 +1,26 @@ +%Part a. +y_analytical = @(t) cos(3*t); + +%Part b. +dt = 0.01; %step size +a = figure(1); +set(0, 'defaultAxesFontsize', 16) +set(0, 'defaultTextFontsize', 16) +set(0, 'defaultLineLineWidth', 2) +t = [0:dt:3]'; +y_n = zeros(length(t), 2); +y_n(1, :) = [1 0]; %initial condition +for i = 2:length(t) + dy = ode3(t, y_n(i-1, :)); + y_n(i, :) = y_n(i-1, :) + dt(k)*dy; +end + + plot(t, cos(3*t), 'k-', t, y_n(:,1), 'o-'); +end + +%Part c. +figure() +dt = 0.001; +t = [0:dt:3]; +y_heun = heun_sol_order2(@(t,y) ode3(t,y), dt, [1 0], [0 3]); +plot(t, cos(3*t), 'k-', t, y_heun(:,1), 'o-'); \ No newline at end of file diff --git a/README.md b/README.md index 2a77514..4dc6d88 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ a = figure(1); set(0, 'defaultAxesFontsize', 16) set(0, 'defaultTextFontsize', 16) set(0, 'defaultLineLineWidth', 2) -hold on t = [0:dt:3]'; y_n = zeros(size(t)); y_n(1) = 1; %initial condition @@ -35,5 +34,52 @@ legend('Analytical', 'Heun', 'Location', 'Northeast') saveas(b, 'heun.png'); ``` -### Part a. -![Analytical vs. Euler](./euler.png) \ No newline at end of file +### Part b. +![Analytical vs. Euler](./euler2.png) + +### Part c. +![Analytical vs. Heun](./heun2.png) + +## Problem 3 + +### Script: +```matlab +%Part a. +y_analytical = @(t) cos(3*t); + +%Part b. +dt = 0.001; %step size +a = figure(1); +set(0, 'defaultAxesFontsize', 16) +set(0, 'defaultTextFontsize', 16) +set(0, 'defaultLineLineWidth', 2) +t = [0:dt:3]'; +y_n = zeros(length(t), 2); +y_n(1, :) = [1 0]; %initial condition +for i = 2:length(t) + dy = ode3(t, y_n(i-1, :)); + y_n(i, :) = y_n(i-1, :) + dt(k)*dy; +end +plot(t, y_analytical(t), 'k-', t, y_n(:,1), 'o-'); +title('Analytical vs. Euler') +legend('Analytical', 'Euler', 'Location', 'Northeast') +saveas(a, 'euler3.png'); + +%Part c. +b = figure(2); +dt = 0.001; +t = [0:dt:3]; +y_heun = heun_sol_order2(@(t,y) ode3(t,y), dt, [1 0], [0 3]); +plot(t, y_analytical(t), 'k-', t, y_heun(:,1), 'o-'); +title('Analytical vs. Heun') +legend('Analytical', 'Heun', 'Location', 'Northeast') +saveas(b, 'heun3.png'); +``` + +### Part b. +![Analytical vs. Euler](./euler3.png) + +### Part c. +![Analytical vs. Heun](./heun3.png) + +## Problem 4 \ No newline at end of file diff --git a/euler.png b/euler.png deleted file mode 100644 index d929569..0000000 Binary files a/euler.png and /dev/null differ diff --git a/euler3.png b/euler3.png new file mode 100644 index 0000000..43dd612 Binary files /dev/null and b/euler3.png differ diff --git a/heun.png b/heun.png deleted file mode 100644 index cdfc2f6..0000000 Binary files a/heun.png and /dev/null differ diff --git a/heun3.png b/heun3.png new file mode 100644 index 0000000..f20321d Binary files /dev/null and b/heun3.png differ