Class Note for MATH 215 with Professor Dostert at UA 2
Class Note for MATH 215 with Professor Dostert at UA 2
Popular in Course
Popular in Department
This 5 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Arizona taught by a professor in Fall. Since its upload, it has received 15 views.
Reviews for Class Note for MATH 215 with Professor Dostert at UA 2
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
An Introduction to Matlab Part 5 This lecture assumes that you have already worked through parts 14 You should be able to create and use script les create and use vectors create and use matrices and understand the concept of component wise arithmetic This part covers 0 Plotting in 2D 0 Plotting in 3D Plotting in 2D There is really one main point you need to understand in order to plot in Matlal everything should be de ned pointwise When Itell an application such as Maple or Mathematica or your calculator to plot a function such asy 12 for 0 S I S 1 what does it do It take a bunch of points likely a few hundred in between 0 and 1 plugs them into the equation then plots points xi in order connecting each point with a straight line The effec you get is a nice smooth graph since so many points were used The big difference in Matlah is that we need to explicitly set up these points that are to be plotted 1 Setting up the domain Here we learn a few ways to set up the domain for a 2D plot a l 0 Open Matlal If you already have it open type clear all in the Command Window Let us take the simple example of plotting y 12 for 0 S I S 1 Our goal is to set up the domain 0 S I S 1 As mention hefore Matlah will only plot a sequence of point we must set up a sequence of points from 0 to 1 Try a linspaoow 1 This assigns z to he a vector from 0 to 1 that has 100 points so I 0 set up our own stepping for I by typing a 0 99 Note that these two methods are equivalent 1 98 E 1 We could alternatively If you wish to have more or less points in your plot then you can give linspace a 3Ml argument specifying the number of points Try a linspaoo0110 and a linspaoowJJOOO 2 Setting up the Tango Here we discuss how to set the range once the domain for the 2D plot is de ned a l We assume that the domain of the plot is already set as a vector as To plot y 12 we need to set up another vector y which contains the values of the z vec or each squared So we type y 13972 Recall that we must include the here since we wish to square each component of z and 12 does not make sense for a vector 1 This same idea holds regardless of the function you wish to plot but you need to be careful with componentwise sm I arithmetic Recall that x3 will be 371 something like will he 5ina and so on 3 Plotting a single function Here we set up the domain range and plot a few functions We discuss some common plot options a Let us plot y 12 for 0 S I S 1 using 5 points 100 points and 1000 points We must set up the domain range and then use the plot command For 5 point we do a linspaco015 y 13972 plotary When plot is called a new window a gure window opens and a blue line connecting our 5 coordinates is displayed Of cours this plot is a very rough estimate of y 12 since we are only using 5 points Try plotting using 100 points and 1000 points l C d C We can skip the intermediate step of de ning y simply by typing plotaa 2 Try this now Try plotting y 13 y sin I and y e for 0 S I S 1 You can change the color and style of your plot with an extra argument to the plot command Type help plot for information on the available options For now try the following and attempt to understand what they are doing a li7l5pdco0110 Pl0tf17flf 2 r Pl0tf17flf 2 7km plotara 2 c v w 7 Pl0tf17flf 2 mz 7 BIG NOTE We have not nor will not discuss how to plot implicit functions such as 12 y2 1 I would suggest you simply do this piecewise for now like a li7l5pdco 11 plotar5qrt1ar 2 b a5q39rf1ar 2 b If you need to use something more complicated I would suggest you convert the equation to parametric form 4 Plotting multiple functions We can plot multiple functions on one single graph one of two ways The rst involves a single long plot command the second involves multiple separate plot commands a l 0 Let us plot y 12 y sinz ND y e all on the same gure for 0 g I g 1 To do this without options is surprisingly easy We simply type each set of coordinates one after another a li7l5pdco0110 Pl0tf17flf 2a5inadarowad The problem with the above is that its not terribly easy to see which plot is which Luckily we can after each plot Try Pl0tf17flf 2 b a5inar r aroa1arior The second way to plot multiple functions is slightly more complicated We use a function called hold to specify that we wish to hold a gure in other words this says not to replace my current gure but add to it instead We can turn hold on or hold off By default hold is off Try a li7l5pdco0110 Pl0tf17flf 2 b 7 hold on plotar5inar quotr Plotlmwlw 31quot Note that we do not need a second hold on in order to plot the third function This is because once hold is turned on it will only be turned off when we specify hold off Keeping the previous gure open try hold off Pl0tf17flf 2 b 7 What happens Well the new plot command overwrite the old gure since hold is turned off any speci c add options Let us attempt now to plot a pic quotwise function Let us say we wish to plot y z for 0 g I g 1 and y 13 for 1 lt I S 2 How would we do this The same way as above but we have two different domains We re going to use a little trick with plotting y I Since Matlab is going to plot a straight line between any two points we donquott really need to use linspace and ploy y z for 100 points in between 0 and 1 We simply need to plot a line from the rst point 00 to the last 11 Try a linspdoo12 This sets up the domain for 1 plot0101arar 3 5 Annotating jigarcs We learn how to add axis labels a title and a legend to a given plot a The rst method to annotate plots is by simply using the graphical interface in the gure window Plot y x2 with a solid blue line and y sinz with a dotted red line for 0 g I g 1 together using a single plot command the rst method from the previous section At the gure window go to the insert menu Note there is an option l C to insert labels a title and a legend Using this menu label the 139axis z the y axis y and title the plot Plot of 1 2 vs s39in139 Matlab should correct your 1 2 to 12 Go to insert gtlegend It should display a legend box with names data and data24 Change data to 1 2 and data2 to sin above by using command line arguments Close your previous plot and replot y 12 sinz with a dotted red line for 0 g I g 1 together using a single plot command You can also do each of the with a solid blue line and y Try 139label 1quot ylabel y 7 title Plot of 1 2 vs sin139 legend 1 2 s in1 We have the same result as when using the graphical interfz Note that legend has two separate string arguments passed into it since we are plotting two functions If we are plotting four functions it has four argument so on The advantage of annotating this way is that I can put all of this information into a function or a and if I change anthing in my plot I can rerun the script and not have to bother going into the insert menu ev single time There are some more options to each of these commands Iwould suggest you use the help menu if you need more advanced features 64 Earpo rt39ing printing or copying and pasting gures We discuss some methods that you may use to attempt to get your gure out of Matlab and into something else If you are able to simply copy and paste gures do this and ignore much of the things below a l 0 Getting a gure out of Matlab and into another program is a surprisingly complex subject It varys across each platform Linux Mac Windows and often things do not work nearly as well as expected For this class the easiest thing to do is attempt to copy your gure and paste it into a MS Word type program On some systems this is as easy as going to edit gtcopy then going to paste in your o ice program Sometimes copying and pasting either does not work or is not available as an option this happens often in Linux The easiest thing to do at this point is to export your gure as a picture lel You are likely most familiar with the JPEG format This should be ne for the type of gures we will use in this class In the gure window if you donquott have a gure window plot something go to File gtSave As You should be able to pick a location to save you lel By default Matlab should try to save your gure as a g This is a native Matlab format and will be able to be opened by ONLY Matlab You can change the Files Of Type option in the save window to whatever you would like such as ljpg Then type the name of the le you want to savel I would ALWAYS include the su ix on the le I would expli itly type in for example PaulsPlotjpg if I had chosed a JPEG le Then in your o ice program simply insert the JPEG as a picture lel If you decide to use Matlab for more than an occasional picture or two for your class assignment I would suggest always saving two copies of a gure One in native Matlab g format and another as whatever type you prefer such as JPEG The reason for this is sometime it may take you an hour or so to go through all of the work to obtain a nice plotl If you save the plot as a JPEG then decide later on your wish to change the plot for example maybe your title was Plot of two functions and you decided it should be Plot of 2 Functions then you CANNOT change the JPEG le easily You can howev open up a gure window in Matlab tell it to open your g le then change the title and re save the JPEG with the new title Plotting in 3D Plotting in 3D uses the same ideas and plotting in 2D For 2D we set up a sequence of points in 1D for example I 1 93 E EA then we applied a function to these 1 values such as y 1 2 and we plotted the resulting 2D coordinates What would be the analog for 3D 14 3D space curves We plot space curves parameterized by a single parameter t a Recall that parametrically a space curve in 3D can be written for some t as I f0 90 ht For example we can plot a helix parametrically by having I sint y cost and 2 it Just as with 2D we must set the t domain Let us say we wish to plot this helix for 0 S t S 10 Then we set t linspaccmO To plot a 1D object in 3D a space curve is simply a 1D curve in 3D space we use the plot function The idea is this is the 3D analog of plot which recall plots a 1D object in 2D space It takes the z y and 2 coordinates of the parameterization as arguments plot s m costt One of the advantages to plotting in Matlab is that we can now play with our 3D object Go to the gure window and click on the icon that looks like a counterclock ise arrow wrapped around a cube it is called Rotate 3D when you move your mouse on it Then go to the plot ick and hold your mouse and move the mouse around You ll see that you can rotate the plot and look at if from any angle you desire Try plotting z sin7rt y 0057rt and 2 t Change it so that you look at the plot from above What does it look like b Any parameterization with 1 parameter and 3 coordinates can be done like this Try plotting cost 66051 7 2 0054t 7 sin5t12 sint 66 50 7 2 0054t 7 sin5t12 2t for 0 S t S 10 Look at this plot from above pretty neat isn t it 2 3D surfaces Say we wish to plot a plane 2 z y for 0 S Ly S 1 You could think we set I 0 y 0 1 and then plot 2 arv wy but what is z y It s simply the vector 1 y 0 22 So this would just plot a straight line like in the plot3 command The idea for plotting a surface is that we need an underlying 2D mesh just like to plot in 2D we need an underlying 1D object in 3D we need an underlying 2D object Then we apply a function to this mesh Think of it as creating a mesh in my space then for each point in this mesh we have a height 2 This creates a surface a Let us plot 2 z y for 0 S I y S 1 We rst set up the mesh In order to do this we specify the z and y ranges using linspace a li7 spacc0110 y linspacc0110 So this sets up I and y as 1 dimensional objects that go from 0 to 1 with points What do we want We want every single combination of these objects So we want y going from 0 to 1 with points for z 01 19 etc So we want 10 X 10 points in 2D given by 1 yj where z j 1 10 The way to set this up is to use the moshg39m39d command Type X Y vrwshgrid m This returns MATRICES X and Y which contain the z coordinates and y coordinates of the 10 X 10 mesh respectively Look at what is actually contained in X and Y b Now what do we want to plot We want a Z value that is de ned at each 10 X 10 2D points in the mesh So we type Z My Where we are using the matrices X and Y and no longer the vectors 1 y c To actually plot we have a few choices The easiest choices are the surf or mesh commands Try surfXYZ We get a nice plane in 3D space with the color proportional to the height Now try mcsMX KZ We get something similar but the interior of each quadrilateral is not colored only the edges are Generally both of these methods will return nice plots it is just a matter of your preference d There is another choice for plotting which is sometimes more desireahle You can plot contours in 2D instead of a 3D plot Try contourX KZ colorba39r This gives us a 2D plot with colored lines representing the height of the plane The colorba39r command adds the color label on the right hand side of the plot so that you know what value each color corresponds to e You can also plot parametric surfaces Say we wish to plot 1u7 v uv sin 151 y u7 v uv cos 15v 2 u7 v v for 71 S u v S 14 We use the same ideas setting up u and v vectors setting up a mesh with matrices U and V then plotting the X Y and Z values according to the parametric equations u linspacc 11 v linspacc11 LEV vrwshgride X U V539m15V Y U V 00515V Z V mcsMX KZ 34 Other notes Pretty much everything that we used for 2D plots also works for 3D We can annotate the same we now have a zlabcl function print the same and export the same The big difference is in plotting multiple plots You CA NOT enter su39r X YXVYX XX 2v w Y 2 In this case you will get an error You MUST use the hold on method to plot multiple plots on a single gure for 3D gures
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'