Basic MATLAB
-
Upload
kaden-puckett -
Category
Documents
-
view
26 -
download
0
description
Transcript of Basic MATLAB
![Page 1: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/1.jpg)
Basic MATLAB
![Page 2: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/2.jpg)
Matlab
• Post-processer
• Graphics
• Analytical solution comparisons
![Page 3: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/3.jpg)
Vectors>> a=[1 2 3 4]
a =
1 2 3 4
>> a'
ans =
1 2 3 4
![Page 4: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/4.jpg)
Autofilling and addressing Vectors
> a=[1:0.2:3]'
a =
1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.2000 2.4000 2.6000 2.8000 3.0000
>> a(2:3)ans =
1.2000 1.4000
![Page 5: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/5.jpg)
xy Plots
>> x=[1 3 6 8 10];
>> y=[0 2 1 3 1];
>> plot(x,y)
![Page 6: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/6.jpg)
Matrices>> b=[1 2 3 4;5 6 7 8]
b =
1 2 3 4 5 6 7 8
>> b'
ans =
1 5 2 6 3 7 4 8
![Page 7: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/7.jpg)
Matrices
>> b=2.2*ones(4,4)
b =
2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000 2.2000
![Page 8: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/8.jpg)
Reshape>> a=[1:9]
a =
1 2 3 4 5 6 7 8 9
>> bsquare=reshape(a,3,3)
bsquare =
1 4 7 2 5 8 3 6 9
>>
![Page 9: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/9.jpg)
Load and if
• a = load(‘filename’); (semicolon suppresses echo)
• if(1)
…
else
…
end
![Page 10: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/10.jpg)
For
• for i = 1:10
• …
• end
![Page 11: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/11.jpg)
Grad
[dhdx,dhdy]=gradient(h);
![Page 12: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/12.jpg)
BMP Output
bsq=rand(100,100);
%bmp1 output
e(:,:,1)=1-bsq; %r e(:,:,2)=1-bsq; %g e(:,:,3)=ones(100,100); %b imwrite(e, 'junk.bmp','bmp');
image(imread('junk.bmp')) axis('equal')
![Page 13: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/13.jpg)
Contours
• h=[…];
• contour(h)
• [C,H]=contour(h)
• Clabel(C,H)
![Page 14: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/14.jpg)
Contours (load data, prepare matrix)
• rho=load('rho_frame0012_subs00.dat')• p_vector=rho/3• rows=100• columns=20
• for j=1:columns• j• for i=1:rows• p(i,j)=p_vector(j+(i-1)*columns); • • %Get rid of '0 pressure' solids that dominate pressure field• if p(i,j)==0• p(i,j)=NaN;• end• end• end
![Page 15: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/15.jpg)
Contours
• [C,H]=contour(p)
• clabel(C,H)
• axis equal
![Page 16: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/16.jpg)
Quiver (vector plots)
>> scale=10;
>> d=rand(100,4);
>> quiver(d(:,1),d(:,2),d(:,3),d(:,4),scale)
![Page 17: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/17.jpg)
Quiver (and Quiver3)• uv_vector=load('u_frame0012_subs00.dat')
• rows=100• columns=20• for j=1:columns• j• for i=1:rows• u(i,j)=uv_vector(j+(i-1)*20,1); • v(i,j)=uv_vector(j+(i-1)*20,2);
• end• end
• hold on• quiver(u,v)
![Page 18: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/18.jpg)
Streamline
• [Stream]= stream2(u,v,5,100)
• streamline(Stream)
![Page 19: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/19.jpg)
Streamlines
• [Stream]= stream2(u,v,[1:20],100*ones(20,1));
• streamline(Stream)
• (This is for streamlines starting at y = 100 from x = 1 to 20 along the x axis. Different geometries will require different starting points.)
![Page 20: Basic MATLAB](https://reader035.fdocuments.in/reader035/viewer/2022072015/56813060550346895d962a98/html5/thumbnails/20.jpg)