# Matlab ## MATLAB Plotting figure template ![](https://imgur.com/rJVAxIv.gif) [**Watch video**](https://drive.google.com/file/d/1MVmAe-eKLOxaSn4yZlOx9N-P2g7QG0aG/view?usp=sharing) :::spoiler ```xml= figure(12); plot(t(:,1), t(:,2), t(:,1), t(:,3),t(:,1), t(:,4),'LineWidth',2,'LineStyle','--'); grid on, hold on; xlabel('Angles of Attack','FontName', 'Times'); ylabel('Cl/Cd','FontName','Times'); title('Cl/Cd v Angles of Attack','FontName','Times'); legend({'DU 06 W 200', 'Spanwise-S1(The riblets above the surface)', 'Spanwise-S1 (The riblets under the surface)'},'FontName','Times') set(gca,'FontSize',12); set(gca,'FontName','Times'); set(gca,'FontName','Times','FontSize',12,'XTick',... [0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30]); ``` ```xml= %% ----------Plot x vs y--------------- figure(1); plot(data(:,1), data(:,2), data(:,1), data(:,3), data(:,1), data(:,4), 'LineWidth', 2, 'MarkerSize', 4); xlabel('length along surfaces'); ylabel('local Nusselt number'); grid on, hold on; title('local Nusselt number along length surfaces'); legend({'Ri 0.1', 'Ri 1.0', 'Ri 10.0'},'Location','northeast') set(gca,'FontSize',12); set(gca,'FontName','serif'); set(gca,'FontWeight','bold'); set(gca,'LineWidth',2); set(gcf, 'Renderer', 'painters'); ``` ```xml= %% subplot figure(2); subplot(2,1,1) plot(data(:,1), data(:,2), 'LineWidth', 2, 'MarkerSize', 4); xlabel('length along surfaces'); ylabel('local Nusselt number'); grid on, hold on; title('local Nusselt number along length surfaces'); legend({'Ri 0.1', 'Ri 1.0', 'Ri 10.0'},'Location','northeast') set(gca,'FontSize',12); set(gca,'FontName','serif'); set(gca,'FontWeight','bold'); set(gca,'LineWidth',2); set(gcf, 'Renderer', 'painters'); subplot(2,1,2) plot(data(:,1), data(:,4), 'LineWidth', 2, 'MarkerSize', 4); xlabel('length along surfaces'); ylabel('local Nusselt number'); grid on, hold on; title('local Nusselt number along length surfaces'); legend({'Ri 0.1', 'Ri 1.0', 'Ri 10.0'},'Location','northeast') set(gca,'FontSize',12); set(gca,'FontName','serif'); set(gca,'FontWeight','bold'); set(gca,'LineWidth',2); set(gcf, 'Renderer', 'painters'); ``` ::: ![](https://i.imgur.com/0aZDC6P.jpg) :::info Untuk bagian edit, saya edit di paint tapi lebih bagus di inkscape. Info dari mas zee bagusnya disimpan format "eps atau svg" supaya bisa di edit di inkscape, tapi saya simpan format picture "jpeg". ::: **Untuk mengedit garis grafik** ![](https://i.imgur.com/2g5YaVX.png) **Untuk mengedit data di dalam grafik** ![](https://i.imgur.com/oFXtjXa.png) ![](https://i.imgur.com/Hax33FY.png) **Notes** 1. Untuk legend code nya bisa langsung di hapus, karena pada menu **Insert > Legend**, dapat menambahkan legend serta mengedit dengan double klik pada legend nya. ![](https://i.imgur.com/51tnAzM.png) ![](https://i.imgur.com/CVfc5bi.png) ## Sumber :::info * [**Mas Zee - CODE Template**](https://hackmd.io/@libernormous/code_temp#) * [**Subplot: Created axes in tiled positions**](https://www.mathworks.com/help/matlab/ref/subplot.html#d123e1376274) * [**2D Plotting in Matlab](https://www.youtube.com/watch?v=gDmpqn92s5U) * [**3D Plotting in Matlab**](https://www.youtube.com/watch?v=OUwfE_-tcfo) Untuk fungsi plot "patch, mesh, surf, dan contour", bagian Z itu harus berupa matrix bukan scalar atau vector. Fungsi "hold on" supaya tipe plotting bisa ada dua dalam satu gambar (figure). ::: :::spoiler ```xml= %% ----------Plot x vs y vs z--------------- figure(6); plot3(y2(:,2), y2(:,3),y2(:,4)); xlabel('Plain NACA 0012'); ylabel('Spanwise NACA 0012'); zlabel('Streamwise NACA 0012'); grid on, hold on; ``` ::: ## Heat Transfer 2D - Finite Difference Method ![](https://i.imgur.com/J8OLNIH.jpg) ::: spoiler ```xml= %% Geometric Domain W = 1; %Width of domain (in m) H = 1; %Height of domain (in m) Nx = 51; %Number of grid points in X direction Ny = 51; %Number of grid points in Y direction dx = W/(Nx-1); %Length of elements in X direction (in m) dy = H/(Ny-1); %Lenght of elements in Y direction (in m) %% Boundary and Initial Conditions T = 25*ones(Nx,Ny); %Temperature Array (in C) TL = 120; %Left wall temperature (in C) TB = 40; %Bottom wall temperature (in C) TR = 40; %Right wall temperature (in C) TT = 40; %Top wall temperature (in C) T(1,2:Ny-1) = TL; T(2:Nx-1,Ny) = TT; T(Nx,2:Ny-1) = TR; T(2:Nx-1,1) = TB; T(1,1) = (TL + TB)/2; T(Nx,1) = (TR + TB)/2; T(1,Ny) = (TL + TT)/2; T(Nx,Ny) = (TR + TT)/2; %% Computation Epsilon = 1e-3; Error = 5; Iter = 0; while(Error>Epsilon) Iter = Iter + 1; disp(Iter); Told=T; for j = 2:Ny-1 for i = 2:Nx-1 T(i,j) = (T(i+1,j) + T(i-1,j) + T(i,j+1) + T(i,j-1))/4; end end Error = max(max(abs(Told-T))); end %% Plotting the Result x = 0:dx:W; %Width array (in m) y = 0:dy:H; %Height array (in m) colormap(jet); contourf(x, y, T'); title ('Temperature Distribution'); xlabel ('Width (in m)'); ylabel ('Height (in m)'); ``` ::: :::info Sources : https://www.mathworks.com/matlabcentral/fileexchange/93215-matlab-code-for-2-d-steady-state-heat-transfer-pdes ::: ## Diffusion Equation 2D - Finite Difference Method ![](https://i.imgur.com/532xsrk.jpg) :::spoiler ```xml= %% Defining mesh n_points = 51; dom_size = 1; h = dom_size/(n_points-1); %% Intialising the problem y(n_points, n_points) = 0; y(1,:) = 1; y_new(n_points, n_points) = 0; y_new(1,:) = 1; Epsilon = 1e-6; Error = 5; Iter = 0; %% Calculations while(Error>Epsilon) Iter = Iter + 1; disp(Iter); y = y_new; for j = 2:(n_points-1) for i = 2:(n_points-1) y_new(i,j) = (y(i+1,j) + y(i-1,j) + y(i,j+1) + y(i,j-1))/4; end end Error = max(max(abs(y-y_new))); end %% Plotting x_dom = ((1:n_points)-1).*h; y_dom = 1-((1:n_points)-1).*h; [X,Y] = meshgrid(x_dom, y_dom); contourf(X,Y,y, 12) colorbar ``` ::: ## Simpulan Berdasarkan dari hasil video [Solving 2D Convection Diffusion using MATLAB | Lecture 13 | ICFDM](https://www.youtube.com/watch?v=RK3_YXumMMQ&list=PLKSR9A4mJH5rvt_Lf007xbmJISRWA0xYS&index=15) Finite Difference Method (FDM) dan Finite Volume Method (FVM) dapat digunakan untuk melakukan simulasi pada kasus teknik. Namun dalam kasus mekanika fluida, terdapat faktor Convection dan Diffusion yang dilambangkan dengan Peclet number. Peclet number is rasio between convection and diffusion, whereas in large number is dimontaed by convetion. Central differencing scheme (FDM) dapat melakukan perhitungan secara numerik, namun pada Pe number yang kecil tidak dapat melakukan di Pe yang besar. Sedangkan, upwind scheme (FVM) dapat melakukan perhitungan numerik, meski nilai Pe nya besar. Oleh karena itu CFD menggunakan FVM dibandingkan FDM. ## Finite Element Method {%youtube WwgrAH-IMOk %}