Week 2 Programming Notes
Week 2 Programming Notes BE1500
Popular in Intro to Programming
verified elite notetaker
Popular in Engineering and Tech
This 13 page Class Notes was uploaded by Neha Bhagirath on Sunday May 22, 2016. The Class Notes belongs to BE1500 at Wayne State University taught by Tonya Whitehead in Spring 2016. Since its upload, it has received 12 views. For similar materials see Intro to Programming in Engineering and Tech at Wayne State University.
Reviews for Week 2 Programming Notes
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: 05/22/16
Anonymous Functions and Plotting Syntax is “plot( x, y)” ex . X=1:10 Y=01:0:1.1 when you are graphing make sure you use the same number of x and y coordinates Multiple Plotting on the same graph: Ex. plot(X,Y,'ro') Adding Titles and Axis Labels title(‘string’) xlabel(‘string’) ylabel(‘string’) legend(‘string’) ex. legend(‘Function 1’, ‘Function 2’) if you have 3 data sets put 3 strings in there, separated by commas if you put y1=x^2, and x=1:10, it will do matrix multiplication so do y=x.^2 instead how to do put your legend in the best place > legend('Data Set 1','Data Set 2','Location','Best') but don’t close the window for the plot or it won’t graph Hold on/hold off: hold on means that you plot the figure in there, then put hold off. THEN graph another graph and it’ll be in another window putting points by hand: use square brackets Homework: Answer: and put hold off after it you can choose where to put the legend: legend('Data Set 1','Location','Best') > instead of best, put n,s,e,w for north south east west if you want to make the same scale to compare: Subplot: multiple graphs in the same figure ex. subplot(2,1,1) means 2 rows, 1 column, and position 1 Homework: Answer: Anonymous Functions: single line equations Ex. f = @(x)x.^(1/3) >> f(8) ans = 2 the (a,2,2) means that a is from 010 and b and c equal 2 always And its @a because we use a for our variable Homework: Answer: a) >> Test1=@(x)(cos(x).^(5/2))*(x4).^2x.^(1/3) Test1 = @(x)(cos(x).^(5/2))*(x4).^2x.^(1/3) >> fplot(@(x) Test1(x),[0,pi/2]) b) when you have 2 vectors, use plot function Answer: Day 4 The simplest type of MATLAB program is called a script, has a .m extension. You can run a script by typing its name at the command line Anonymous Functions: not stored in a program file, but is associated with a variable whose data type is function_handle. Can contain only a single executable statement. Practice: >> R=[0, pi/8, pi/6, pi/4, pi/2] R = 0 0.3927 0.5236 0.7854 1.5708 >> function_name=@(x)(sin(x)).^2+((cos(x)).^2./(sin(x)cos(x))) % DOT BEFORE DIVISION function_name = @(x)(sin(x)).^2+((cos(x)).^2./(sin(x)cos(x))) >> B=function_name(R) B = 1.0e+15 * 0.0000 0.0000 0.0000 4.5036 0.0000 New question: Answer: >> A=1:10 A = 1 2 3 4 5 6 7 8 9 10 >> f=@(x)3*x.^2+5 f = @(x)3*x.^2+5 >> f(A) ans = 8 17 32 53 80 113 152 197 248 305 >> Y=f(A) Y = 8 17 32 53 80 113 152 197 248 305 >> subplot(3,1,1) >> plot(A,Y) >> f=@(x)12*x(5*x.^2) f = @(x)12*x(5*x.^2) >> f(A) ans = 7 4 9 32 65 108 161 224 297 380 >> Y2=f(A) Y2 = 7 4 9 32 65 108 161 224 297 380 >> plot(A,Y2) >> subplot(3,1,1) >> >> plot(A,Y) >> subplot(3,1,2) >> plot(A,Y2) >> f=@(x)x.^3+(2*x.^2)(3/(xx.^2)) f = @(x)x.^3+(2*x.^2)(3/(xx.^2)) >> f(A) f = @(x)x.^3+(2*x.^2)(3./(xx.^2)) >> f(A) ans = 1.0e+03 * Inf 0.0175 0.0455 0.0963 0.1752 0.2881 0.4411 0.6401 0.8910 1.2000 >> Y3=f(A) Y3 = 1.0e+03 * Inf 0.0175 0.0455 0.0963 0.1752 0.2881 0.4411 0.6401 0.8910 1.2000 >> subplot(3,1,3) >> plot(A,Y3) plot has to have a vector, and fplot can have a vector or a function you can also use fplot for functions with multiple variables, however you have to put individual values in for all but 1 variable Ex. f=@(a,b,c)a.^2+b.^2+c.^2
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'