From d3cadd62028b5f863d83b7db79e234f34cf2c0d6 Mon Sep 17 00:00:00 2001 From: "g.mandl" Date: Wed, 14 Jan 2026 18:42:43 +0100 Subject: [PATCH] =?UTF-8?q?Dateien=20nach=20=E2=80=9ESheet=5F6=E2=80=9C=20?= =?UTF-8?q?hochladen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sheet_6/ex_6_a.m | 80 ++++++++++++++++++++++++++++++++++++++++++++ Sheet_6/ex_6_b.m | 71 +++++++++++++++++++++++++++++++++++++++ Sheet_6/ex_6_c.m | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 Sheet_6/ex_6_a.m create mode 100644 Sheet_6/ex_6_b.m create mode 100644 Sheet_6/ex_6_c.m diff --git a/Sheet_6/ex_6_a.m b/Sheet_6/ex_6_a.m new file mode 100644 index 0000000..a5ebd44 --- /dev/null +++ b/Sheet_6/ex_6_a.m @@ -0,0 +1,80 @@ +% Sheet 6 / Ex A +clf, clear, close all + +% you need to play around with the parameters (enough starting nodes, +% refinements etc) + +%p_vec = [5,10,20,100]; +p = 100; % choose your p +n_nodes = 10; % number of nodes on the coarse mesh (even - x=0 not included, odd - x=0 included) +nodes = linspace(-1,1,n_nodes); +f = @(x) 2*p^3.*x./(p^2.*x.^2+1).^2; +u_exact = @(x) atan(p*x); +lambda = @(x) 1+0*x; + +% h-adaptivity +it_h = 4; % number of refinements with h-adaptivity +for it = 1:it_h + [K,f_vec] = assembling(nodes,lambda,f); + % adaption for boundary (dirichlet left) + f_vec(1) = f_vec(1) + K(1,1)*1e6*(-atan(p)); + K(1,1) = K(1,1)*(1+1e6); + % adaption for boundary (neumann right) + f_vec(end) = f_vec(end) + p/(p^2+1); + + % solving the system + u = K\f_vec; + + % adapting the mesh + if it < it_h + nodes = adapt_h(nodes,u,lambda); + end +end + +figure(1) +hold on +plot(nodes,u,'-o') +fplot(u_exact,[-1,1]) +title(['h-adapt: u_h, u for p=' num2str(p) ' after ' num2str(it_h) ' refinements, nstart=' num2str(n_nodes) ', nend=' num2str(length(nodes))]); +xlabel('x values'); +ylabel('u'); +legend('u_h', 'u'); +grid on; +hold off +file_name_1 = ['ex_6_A_h-adap_p' num2str(p) '_ref' num2str(it_h) '_n_nodes' num2str(n_nodes) '.jpg']; +saveas(figure(1), file_name_1); + + +% r-adaptivity +n_nodes_r = 20; % number of nodes on the coarse mesh (even - x=0 not included, odd - x=0 included) +nodes_r = linspace(-1,1,n_nodes_r); +it_r = 3; % number of refinements with r-adaptivity +for it = 1:it_r + [K,f_vec] = assembling(nodes_r,lambda,f); + % adaption for boundary (dirichlet left) + f_vec(1) = f_vec(1) + K(1,1)*1e6*(-atan(p)); + K(1,1) = K(1,1)*(1+1e6); + % adaption for boundary (neumann right) + f_vec(end) = f_vec(end) + p/(p^2+1); + + % solving the system + u_r = K\f_vec; + + % adapting the mesh + if it < it_r + nodes_r = adapt_r(nodes_r,u_r,lambda); + end +end + +figure(2) +hold on +plot(nodes_r,u_r,'-o') +fplot(u_exact,[-1,1]) +title(['r-adapt: u_h, u for p=' num2str(p) ' after ' num2str(it_r) ' refinements and n=' num2str(n_nodes_r)]); +xlabel('x values'); +ylabel('u'); +legend('u_h', 'u'); +grid on; +hold off +file_name_2 = ['ex_6_A_r-adap_p' num2str(p) '_ref' num2str(it_r) '_n_nodes' num2str(n_nodes_r) '.jpg']; +saveas(figure(2), file_name_2); \ No newline at end of file diff --git a/Sheet_6/ex_6_b.m b/Sheet_6/ex_6_b.m new file mode 100644 index 0000000..6949978 --- /dev/null +++ b/Sheet_6/ex_6_b.m @@ -0,0 +1,71 @@ +% Sheet 6 / Ex B +clf, clear, close all + +% you need to play around with the parameters (enough starting nodes, +% refinements etc) + +n_nodes = 10; % number of nodes on the coarse mesh +nodes = linspace(0,1,n_nodes); +f = @(x) 0; +lambda = @(x) (x<1/sqrt(2))*1 + (x>=1/sqrt(2))*10; + +% h-adaptivity +it_h = 10; % number of refinements with h-adaptivity +for it = 1:it_h + [K,f_vec] = assembling(nodes,lambda,f); + % adaption for dirichlet boundary + K(1,1) = K(1,1)*(1 + 1e6); + f_vec(end) = K(end,end)*1e6; + K(end,end) = K(end,end)*(1 + 1e6); + + % solving the system + u = K\f_vec; + + % adapting the mesh + if it < it_h + nodes = adapt_h(nodes,u,lambda); + end +end + +figure(1) +hold on +plot(nodes,u,'-o') +title(['h-adapt: u_h after ' num2str(it_h) ' refinements, nstart=' num2str(n_nodes) ', nend=' num2str(length(nodes))]); +xlabel('x values'); +ylabel('u'); +grid on; +hold off +file_name_1 = ['ex_6_B_h-adap_ref' num2str(it_h) '_n_nodes' num2str(n_nodes) '.jpg']; +saveas(figure(1), file_name_1); + + +% r-adaptivity +n_nodes_r = 10; % number of nodes on the coarse mesh +nodes_r = linspace(0,1,n_nodes_r); +it_r = 3; % number of refinements with r-adaptivity +for it = 1:it_r + [K,f_vec] = assembling(nodes_r,lambda,f); + % adaption for dirichlet boundary + K(1,1) = K(1,1)*(1 + 1e6); + f_vec(end) = K(end,end)*1e6; + K(end,end) = K(end,end)*(1 + 1e6); + + % solving the system + u_r = K\f_vec; + + % adapting the mesh + if it < it_r + nodes_r = adapt_r(nodes_r,u_r,lambda); + end +end + +figure(2) +hold on +plot(nodes_r,u_r,'-o') +title(['r-adapt: u_h after ' num2str(it_r) ' refinements and n=' num2str(n_nodes_r)]); +xlabel('x values'); +ylabel('u'); +grid on; +hold off +file_name_2 = ['ex_6_B_r-adap_ref' num2str(it_r) '_n_nodes' num2str(n_nodes_r) '.jpg']; +saveas(figure(2), file_name_2); \ No newline at end of file diff --git a/Sheet_6/ex_6_c.m b/Sheet_6/ex_6_c.m new file mode 100644 index 0000000..bf07103 --- /dev/null +++ b/Sheet_6/ex_6_c.m @@ -0,0 +1,86 @@ +% Sheet 6 / Ex C +clf, clear, close all + +% you need to play around with the parameters (enough starting nodes, +% refinements etc) + +p = 70; % parameter +%p = -70; + +n_nodes = 10; % number of nodes on the coarse mesh +nodes = linspace(0,1,n_nodes); +f = @(x) 0; +lambda = @(x) 1+0*x; + +% h-adaptivity +it_h = 10; % number of refinements with h-adaptivity +for it = 1:it_h + [K,f_vec] = assembling(nodes,lambda,f); + % need to add the term for phi'*phi (convection term) + conv_loc = p/2*[-1,-1;1,1]; + for k=1:length(nodes)-1 + K(k:k+1,k:k+1) = K(k:k+1,k:k+1) + conv_loc; + end + + % adaption for dirichlet boundary + K(1,1) = K(1,1)*(1 + 1e6); + f_vec(end) = K(end,end)*1e6; + K(end,end) = K(end,end)*(1 + 1e6); + + % solving the system + u = K\f_vec; + + % adapting the mesh + if it < it_h + nodes = adapt_h(nodes,u,lambda); + end +end + +figure(1) +hold on +plot(nodes,u,'-o') +title(['h-adapt: u_h after ' num2str(it_h) ' refinements, nstart=' num2str(n_nodes) ', nend=' num2str(length(nodes))]); +xlabel('x values'); +ylabel('u'); +grid on; +hold off +file_name_1 = ['ex_6_C_h-adap_ref' num2str(it_h) '_n_nodes' num2str(n_nodes) '.jpg']; +saveas(figure(1), file_name_1); + + +% r-adaptivity +n_nodes_r = 20; % number of nodes on the coarse mesh +nodes_r = linspace(0,1,n_nodes_r); +it_r = 3; % number of refinements with r-adaptivity +for it = 1:it_r + [K,f_vec] = assembling(nodes_r,lambda,f); + % need to add the term for phi'*phi (convection term) + conv_loc = p/2*[-1,-1;1,1]; + for k=1:length(nodes_r)-1 + K(k:k+1,k:k+1) = K(k:k+1,k:k+1) + conv_loc; + end + + % adaption for dirichlet boundary + K(1,1) = K(1,1)*(1 + 1e6); + f_vec(end) = K(end,end)*1e6; + K(end,end) = K(end,end)*(1 + 1e6); + + % solving the system + u_r = K\f_vec; + + % adapting the mesh + if it < it_r + nodes_r = adapt_r(nodes_r,u_r,lambda); + end +end + +figure(2) +hold on +plot(nodes_r,u_r,'-o') +title(['r-adapt: u_h after ' num2str(it_r) ' refinements and n=' num2str(n_nodes_r)]); +xlabel('x values'); +ylabel('u'); +grid on; +hold off +file_name_2 = ['ex_6_C_r-adap_ref' num2str(it_r) '_n_nodes' num2str(n_nodes_r) '.jpg']; +saveas(figure(2), file_name_2); \ No newline at end of file