matlab 1 hari.docx

70
TUTORIAL MEMBUAT GRAFIK 3D DENGAN MATLAB Di postingan sebelumnya kita telah banyak membahas bagaimana cara membuat grafik dengan software matlab , tapi kali ini kita mencoba membuat grafik dengan 3D yang menarik dari sebuah fungsi matematika. Seperti kita ketahui bahwa Matlab merupakan bahasa pemrograman level tinggi yang dikhususkan untuk kebutuhan komputasi teknis, visualisasi dan pemrograman seperti komputasi matematik, analisis data, pengembangan algoritma, simulasi dan pemodelan dan grafik-grafik perhitungan.Sehingga inilah yang menyebabkan matlab mudah digunakan dalam pemodelan simulasi kurva ataupun grafik. Berikut cara pembuatan program grafik tiga dimensi ( 3D) pada Matlab 6.5, dimana fungsi matematika dari grafik tersebut adalah Z = (X 2 + Y 2 ): 1. Bukalah aplikasi matlab yang sudah terinstall di PC anda, kemudian pilih >> New M-File (Ctrl-N) 2. Ketikkan script / kode / listing program matlab berikut ini : sumbu_x = -10:1:10;

Upload: wulandari-tri-maharani

Post on 25-Nov-2015

186 views

Category:

Documents


1 download

DESCRIPTION

TUTORIAL MEMBUAT GRAFIK 3D DENGAN MATLAB materi pengenalan komputer

TRANSCRIPT

TUTORIAL MEMBUAT GRAFIK 3D DENGAN MATLAB Di postingan sebelumnya kita telah banyak membahas bagaimana cara membuat grafik dengan software matlab, tapi kali ini kita mencoba membuat grafik dengan 3D yang menarik dari sebuah fungsi matematika. Seperti kita ketahui bahwa Matlab merupakan bahasa pemrograman level tinggi yang dikhususkan untuk kebutuhan komputasi teknis, visualisasi dan pemrograman seperti komputasi matematik, analisis data, pengembangan algoritma, simulasi dan pemodelan dan grafik-grafik perhitungan.Sehingga inilah yang menyebabkan matlab mudah digunakan dalam pemodelan simulasi kurva ataupun grafik. Berikut cara pembuatan program grafik tiga dimensi ( 3D) pada Matlab 6.5, dimana fungsi matematika dari grafik tersebut adalah Z = (X2 + Y2):1. Bukalah aplikasi matlab yang sudah terinstall di PC anda, kemudian pilih >> New M-File (Ctrl-N)2. Ketikkan script / kode / listing program matlab berikut ini :sumbu_x = -10:1:10; sumbu_y = -10:4:10; [X,Y] = meshgrid(sumbu_x,sumbu_y);Z = X.^2 + Y.^2;mesh(X,Y,Z);3. Kemudian Save dan Run (F5), Berikut hasil grafiknya :

Gambar Grafik tiga dimensi (3D) pada matlab dengan perintah mesh4. Setelah berhasil coba pada tahap kedua ganti kode mesh dengan surf. Maka berikut grafik hasilnya :

Gambar Grafik tiga dimensi (3D) pada Matlab dengan perintah surf5. Dan anda juga dapat merubahnya dengan perintah contour. Grafik akan menjadi seperti ini :

Gambar Grafik tiga dimensi (3D) pada Matlab dengan perintah contour6. Good Luck.!!!

Notes : Penjelasan Program Grafik tiga dimensi (3D) pada tahap kedua diatas :Baris 1 : batas nilai x yang akan di plot Baris 2 : batas nilai y yang akan di plotBaris 3 : mengisi bidang X dan Y dengan jalinan titikBaris 4 : fungsi matematika yang akan diplot yaitu Z = (X2 + Y2)Baris 5 : Perintah program Matlab untuk menampilkan grafik 3D

Surfaces and Grids in MatlabThe ability to handle surfaces and grids is one of Matlabs many strengths. Since a lot of geologic data is naturally of more than one dimension, we have use for these capabilities. SurfacesA surface is a set of polygons. They are easy to plot in Matlab.Because data is often not regularly sampled, surfaces are often modeled as a set of interlocking triangles. There is a particularly compact way of making surfaces of irregularly spaced data called a Delaunay triangulation. Delaunay triangulation is the set of triangles built by connecting each of the points of an irregularly spaced data set where the vertices of the triangles are the data points. Thus it is an exact representation of your known data and linear interpolation in between.To see what we mean, load up the dmap.dat data file from the Q:\geo185 directory by typing load dmap.datThis is an X,Y,Z elevation data file for columns 1,2,3.First visualize the file with the plot3 command>> p_h=plot3(dmap(:,1),dmap(:,2),dmap(:,3),'o') The 'o' parameter plots the data points as individual circles as opposed to a 3D-line.The p_h is called a handle. You could have just typed plot3 without the p_h but by adding this you have the ability to later delete this graphic element >> hold onThis keeps the plot held for more plottingNow in order to plot the data as a triangulated surface we need to build the surface first with:>>tri=delaunay(dmap(:,1),dmap(:,2))This creates a n x 3 matrix of the vertice row entries that are needed for each triangleto visualize this triangulated surface type:>> trisurf(tri,dmap(:,1),dmap(:,2),dmap(:,3))

Note that the vertex of each triangle is one of the data points.If you want to see the plot without the points type:>>delete(p_h)This surface plot is efficient but it doesnt produce a very pretty picture (well at least for this small data set, for large data sets, triangulated surfaces are often preferable because they are more compact and there is enough data that the surface looks good).GridsSo we often want a regular spacing of the X and Y locations to make it smoother and not faceted. A regularly spaced data set is called a grid. It uses up more memory and thus is slower to manipulate because data is defined at every location. To convert the irregularly spaced data to regularly spaced we need to grid it. This requires a couple of steps. First you need to define the spacing and extent of the grid by making two vectors of the X and Y upper and lower limits at a given spacing:>> rangeY=floor(min(dmap(:,2))):.2:ceil(max(dmap(:,2)))>>rangeX=floor(min(dmap(:,1))):.2:ceil(max(dmap(:,1)))0.2 is the spacing I have chosen. I was a little obscure in choosing the floor(min syntax. Try min(dmap(:,2)) to see what that gives you and then try floor(min(dmap(:,2))) to see what that does. Hint: it is a way of controlling the rounding direction.Now make matrices for each X and Y location:>>[X,Y]=meshgrid(rangeX,rangeY)And then interpolate your Z values across this grid:>>Z=griddata(dmap(:,1),dmap(:,2),dmap(:,3),X,Y)To see the result:>>surf(X,Y,Z)

For a smoother interpolation add the cubic spline parameter:>>Z=griddata(dmap(:,1),dmap(:,2),dmap(:,3),X,Y,'cubic')>>surf(X,Y,Z)Note that the grid values in between the data points vary smoothly and no longer have the facets we saw with the triangulated surface.If you want to make a contour plot of the gridded data you can do this by first defining the range and spacing of the Z values:>>rangeZ=floor(min(dmap(:,3))):10:ceil(max(dmap(:,3)))Then running:>>[C,h]=contour(X,Y,Z,rangeZ)

This makes a 2-D contour plot. Again you could left off the [C,h] as this is a handle, but it is useful for labeling the contour lines:>>c_h=clabel(C,h,rangeZ(1:2:end))I chose to take every other value within rangeZ

You can actually combine contours and surfaces and make your surface plot look better and with no grid lines:>>surf(X,Y,Z,'EdgeColor','none','FaceColor','interp','FaceLighting','phong')And then add 3-D contour lines:>> contour3(X,Y,Z,rangeZ,'k')

There are many more possible plots with surfaces and grids. I strongly urge you to look at the help files.

1. The problem statement, all variables and given/known data

Suppose f(x,y) = sin(3y-x^2+1)+cos(2y^2-2x)a) produce a labeled contour plot for -2 Black holes growing faster than expected>> Digging yields clues: Biologists connect burrowing behavior in mice to genes

Jul5-10, 10:43 PM Re: matlab & saddle point #2

Dick

Recognitions:Homework HelperScience AdvisorYes, it's the green "pointy-gap" parts. And good job on the matlab programming. If you go through on a line through the two green pointy gaps it looks like minimum. If you go through at approximately a 90 degree angle to that it looks like a maximum. Isn't that a saddle point?

Jul5-10, 11:04 PM Re: matlab & saddle point #3

jwxie

@ Dick thank you,

I really have one more simple problem.Code:x= linspace(0, 2, 15);y= linspace(0,2,15);[x y]= meshgrid(x,y);z=sin(3.*x+y)+ 2.*cos(x-y);[C h] = contour(x,y,z);clabel(C,h);

hold onsurfc (x,y,z);view ([1 1 1])title('Yeukhon Wong, Ex. 5.3, Contour Plot of z=(3*x+y)-2*cos(x-y)');hold offoutput:http://i49.tinypic.com/30d8dwj.jpg

This time we are suppose to do the following:Based on the contour plot you found in a) determine whether the function has anycritical points in the square S defined in a). If there are any such points, provideestimates from the graph for their x and y coordinates and provide a justification fromthe graph as to whether these are relative maximum , minimum or saddle points.

I think the darkest red is the maximum line, and its middle point is a good estimation of point of maximum ; the darkest blue is the min line, and the last portion of the line is probably a good estimation of point of minimum . The saddle point, again, is the green pointy-gap. Actually I think a good estimation would be the sharp point.

What do you think? Thank you so much!

from MATLAB Plot Gallery - Surface Contour Plot by Plot Gallery Create a surface contour plot

Surface_Contour_Plot

This is an example of how to create a surface contour plot in MATLAB.Read about the surfc function in the MATLAB documentation.Go to MATLAB Plot Gallery% Create a grid of x and y datay = -10:0.5:10;x = -10:0.5:10;[X, Y] = meshgrid(x, y);

% Create the function values for Z = f(X,Y)Z = sin(sqrt(X.^2+Y.^2)) ./ sqrt(X.^2+Y.^2);

% Create a surface contour plor using the surfc functionfigure;surfc(X, Y, Z);

% Adjust the view angleview(-38, 18);

% Add title and axis labelstitle('Normal Response');xlabel('x');ylabel('y');zlabel('z');

Copyright 2012 The MathWorks, Inc.Published with MATLAB 7.14

Contact us at [email protected]

surfcContour plot under a 3-D shaded surface plot

Syntaxsurfc(Z)surfc(Z,C)surfc(X,Y,Z)surfc(X,Y,Z,C)surfc(...,'PropertyName',PropertyValue)surfc(axes_handles,...)h = surfc(...)Descriptionsurfc(Z) creates a contour plot under the three-dimensional shaded surface from the z components in matrix Z, using x = 1:n and y = 1:m, where [m,n] = size(Z). The height, Z, is a single-valued function defined over a geometrically rectangular grid. Z specifies the color data, as well as surface height, so color is proportional to surface height. surfc(Z,C) plots the height of Z, a single-valued function defined over a geometrically rectangular grid, and uses matrix C, assumed to be the same size as Z, to color the surface.surfc(X,Y,Z) uses Z for the color data and surface height. X and Y are vectors or matrices defining the x and y components of a surface. If X and Y are vectors, length(X) = n and length(Y) = m, where [m,n] = size(Z). In this case, the vertices of the surface faces are (X(j), Y(i), Z(i,j)) triples. To create X and Y matrices for arbitrary domains, use the meshgrid function.surfc(X,Y,Z,C) uses C to define color. MATLAB performs a linear transformation on this data to obtain colors from the current colormap.surfc(...,'PropertyName',PropertyValue) specifies surface Surfaceplot along with the data.surfc(axes_handles,...) plots into the axes with handle axes_handle instead of the current axes (gca).h = surfc(...) returns a handle to a Surfaceplot graphics object. ExamplesDisplay a surface plot and a contour plot of the peaks surface. [X,Y,Z] = peaks(30);surfc(X,Y,Z)colormap hsvaxis([-3 3 -3 3 -10 5])

More Aboutexpand allTipsAlgorithms Representing a Matrix as a Surface Coloring Mesh and Surface PlotsSee Alsoaxis | caxis | colormap | contour | delaunay | imagesc | mesh | meshgrid | pcolor | shading | surf | Surfaceplot Properties | trisurf | view

ezmeshEasy-to-use 3-D mesh plotter

Syntaxezmesh(fun)ezmesh(fun,domain)ezmesh(funx,funy,funz)ezmesh(funx,funy,funz,[smin,smax,tmin,tmax])ezmesh(funx,funy,funz,[min,max]ezmesh(...,n)ezmesh(...,'circ')ezmesh(axes_handle,...)h = ezmesh(...)Descriptionezmesh(fun) creates a graph of fun(x,y) using the mesh function. fun is plotted over the default domain: -2 < x < 2, -2 < y < 2. fun can be a function handle or a string (see the Tips section).ezmesh(fun,domain) plots fun over the specified domain. domain can be either a 4-by-1 vector [xmin, xmax, ymin, ymax] or a 2-by-1 vector [min, max] (where min