1.3 Introduction to Scientific Programming in MATLAB
Formal Metadata
Title 
1.3 Introduction to Scientific Programming in MATLAB

Title of Series  
Part Number 
1.3

Number of Parts 
12

Author 

License 
CC Attribution  NonCommercial  ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and noncommercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license. 
Identifiers 

Publisher 

Release Date 
2012

Language 
English

Content Metadata
Subject Area  
Abstract 
00:09 Introduction to Scientific Programming in MATLAB 00:27 Accessing MATLAB 02:56 Free Clone: GNU Octave 03:18 Introducing... OCTAViEw 03:54 Using Vectors 06:13 More Vectors 08:59 Matrices 11:57 Image Processing 16:44 Matrix Indexing 19:25 Back to Plotting 21:12 Plotting Options 22:15 Functions 23:45 Loops 25:09 Conditionals 26:11 Programming the MATLAB Way 29:22 Simple Input/Output 32:35 Other Resources 32:59 Assignment #3: Spirograph plot

00:00
Computer program
Game controller
State of matter
Sheaf (mathematics)
3 (number)
Revision control
Goodness of fit
Different (Kate Ryan album)
Ring (mathematics)
Moving average
Cloning
Metropolitan area network
Data type
Scripting language
Metropolitan area network
Simulation
Block (periodic table)
Weight
Debugger
Computer program
Coma Berenices
Computer programming
Type theory
Octave
Website
Right angle
Quicksort
Freeware
02:56
Scripting language
Information management
Scripting language
Freeware
Debugger
Plotter
Machine code
Computer programming
Plot (narrative)
Type theory
Octave
Quantum
Resultant
Data type
Cloning
03:55
Computer program
Pixel
Building
Multiplication sign
Plotter
1 (number)
Set (mathematics)
Icosahedron
Mereology
Cartesian coordinate system
Formal language
Medical imaging
Mathematics
Array data structure
Positional notation
Different (Kate Ryan album)
Semiconductor memory
Military operation
Matrix (mathematics)
Cuboid
Social class
Identity management
Touchscreen
Block (periodic table)
Software developer
Electronic mailing list
Data storage device
Sound effect
Bit
Peg solitaire
Element (mathematics)
Type theory
Process (computing)
Vector space
Octave
Right angle
Procedural programming
Figurate number
Resultant
Row (database)
Point (geometry)
Ocean current
Filter <Stochastik>
Octahedron
Functional (mathematics)
Link (knot theory)
Computer file
Diagonal
Graph coloring
Number
Sound effect
Revision control
Frequency
Term (mathematics)
Octave
Operator (mathematics)
Vector graphics
Konturfindung
Energy level
Nichtlineares Gleichungssystem
Conditionalaccess module
Binary multiplier
Multiplication
Distribution (mathematics)
PoissonKlammer
Weight
Image processing
Computer program
Interactive television
Coma Berenices
Line (geometry)
Calculus
Cartesian coordinate system
Computer programming
System call
Symbol table
Convolution
Word
Personal digital assistant
Function (mathematics)
Vertex (graph theory)
Matrix (mathematics)
16:46
Computer program
Euclidean vector
Length
Plotter
Multiplication sign
Orientation (vector space)
1 (number)
Solid geometry
Mereology
Special unitary group
Subset
Formal language
Medical imaging
Positional notation
Singleprecision floatingpoint format
Matrix (mathematics)
Row (database)
Circle
Pixel
Triangle
Theory of relativity
Electric generator
Point (geometry)
Sampling (statistics)
Sound effect
Bit
Price index
Element (mathematics)
Orbit
Type theory
Data management
Arithmetic mean
Computer configuration
Vector space
Green's function
Figurate number
Rhombus
Row (database)
Point (geometry)
Octahedron
Functional (mathematics)
Connectivity (graph theory)
Computergenerated imagery
Graph coloring
Element (mathematics)
Sound effect
String (computer science)
Green's function
Subject indexing
Konturfindung
Dot product
Sine
Image processing
Computer program
Line (geometry)
Computer programming
System call
Symbol table
Subject indexing
Word
Circle
Personal digital assistant
Function (mathematics)
Vertical direction
Matrix (mathematics)
23:05
NPhard
Computer program
Scripting language
Interior (topology)
Orientation (vector space)
Decimal
1 (number)
ACID
Function (mathematics)
Parameter (computer programming)
Mereology
Formal language
Medical imaging
Inference
Different (Kate Ryan album)
Matrix (mathematics)
Information
Social class
Exception handling
Area
Programming language
Computer virus
Gradient
Variable (mathematics)
Discounts and allowances
Type theory
Process (computing)
Vector space
output
Right angle
Quicksort
Resultant
Row (database)
Filter <Stochastik>
Functional (mathematics)
Implementation
Computer file
MIDI
Letterpress printing
Student's ttest
Supercomputer
2 (number)
Product (business)
Number
Revision control
Programmschleife
Wellformed formula
String (computer science)
Konturfindung
Integer
output
Condition number
Default (computer science)
Multiplication
Distribution (mathematics)
Forcing (mathematics)
Machine code
Line (geometry)
Computer programming
System call
Convolution
Number
Loop (music)
Personal digital assistant
Function (mathematics)
Vertex (graph theory)
Statement (computer science)
Vertical direction
Matrix (mathematics)
32:37
Electronic data interchange
Link (knot theory)
Multiplication sign
Plotter
Spiral
Perturbation theory
Graph coloring
Plot (narrative)
Theory
Number
Facebook
Frequency
Complex number
Different (Kate Ryan album)
Circle
YouTube
Physical system
Personal identification number
Graph (mathematics)
Graph (mathematics)
Computer program
Sound effect
Density of states
TLA <Logik>
Computer programming
Type theory
Vector space
Octave
Euler's formula
Video game
Nichtlineares Gleichungssystem
Pattern language
35:39
Metropolitan area network
Line (geometry)
Plot (narrative)
Arm
Storage area network
Inflection point
Pointer (computer programming)
Uniform resource name
Octave
Videoconferencing
Theorem
Row (database)
Condition number
Scalable Coherent Interface
Newton's law of universal gravitation
36:12
Computer program
Complex (psychology)
Functional (mathematics)
Personal identification number
Computer file
Plotter
Multiplication sign
Real number
Source code
Function (mathematics)
Plot (narrative)
Emulation
Goodness of fit
Sign (mathematics)
Error message
Scalable Coherent Interface
Newton's law of universal gravitation
Default (computer science)
Scripting language
Execution unit
Information
Bit
Machine code
Variable (mathematics)
Inflection point
Type theory
Data management
Error message
Vector space
Personal digital assistant
Large eddy simulation
Function (mathematics)
Uniform resource name
Pattern language
Right angle
Figurate number
Volume
Resultant
Spacetime
39:40
Computer program
Graph (mathematics)
Computer file
Plotter
Structural load
Artificial neural network
Firstorder logic
Computer programming
Inflection point
Right angle
Information
Figurate number
Scalable Coherent Interface
00:00
all of a and it was hot only in simulation and will work for new technology Boardwalk but in this next
00:10
section what I wanna do is go through it Introduction to Programming in matlab so that you can get a flavor if you haven't seen it before how it works and what you can do is kind of an overview of what is a lot of the programs that you may have to work on this summer are all in written in Matlab syntax so this is a useful thing to know the true
00:28
but OK that you can try this out and some you did earlier if you're in a workspace and all you can fire matlab as long as you said you and then a particular version of Matlab use matlab dashed 7 . 11 or whatever and like the other version you use path then you should be able to type matlab and fire up the Matlab doing and that a few problems with that this morning but on 1 of the problem that you can run into is that if you try to do that from offcampus then you know what actually blocks in Matlab weight Matlab using inside nanoHUB is really only for Peru people it's license to produce so we actually have special controls in place to block people from using Matlab on or off campus and that's actually what was happening this warning was actually blocking you guys you know you're on campus and there was some glitch was causing you to be blocked so and so if you're having trouble firing up matlab it's probably because of that blocking that we do but if you fire up Matlab on you can type some simple things like this that is an opensource version I wanna show you which would be good to and instead of Matlab or if you're having trouble with Matlab try firing up new program called octave and again you probably have to say you so that it's on your past minority down your path but try try at a command prompt type active than what you get is sort of the poor man's matlab it's the free version right but there is no fancy gooey and all that stuff but you get a nice command line where you can type matlab syntax so if you want to as we're going along and you can type some stuff at the command prompt you can type the commands and I'm going to show you the only when under use that that's a good idea site users active dash 3 . 2 . 4 that's the newest version of octave higher if you happen to be male has a bunch of different versions and if you're using an older version you can easily be confused so it's a smart idea that type used active dash 3 . 2 . 4 and then if you type active is good to go you get the latest version but active does almost all the stuff that Matlab does it's like a clone in a letter for letter syntax syntax it does plots that way states use the matlab URI is missing the fancy interactive stepbystep debugger and missing Simulink toolboxes so it's not quite as they use matlab but actually does all the work and if you have a script written in Matlab it'll run pretty much in inactive show it's a good good thing to use
03:00
also a tool unveil called active you and it's actually just agree on top of active and so again if you're looking for something to try out the examples where we're going along you can fire of active you so if you look on you know for the tool called active you and then click launch tool will bring up a little so it looks like this and
03:21
on the left hand side here you can type in a little active script and then you click Run Script it'll run this script and plot the results it's not too comprehensive only does like x y plots and stuff like that there are lots of things matlab does that actively doesn't do very well but but it's a good quick and dirty thing where you can type in some Matlab code and click the button and you get a plot of the results and and you know how to debug your code so that's another way to run a slightly better than the command line because at least a dozen applying for you can interactively so you could try the active you out to few wanna try these examples right so 1 way or
03:57
another on a walk you through some examples and you can try type about as we go to see what happens so this is a way create Matlab works and in what call vectors arrays of numbers and MATLAB's very good at handling vectors and this is the way you define a 1 way to define a vector I can create a variable called x axis and I can set it to 1 colon . 1 column 5 and that's the way of telling that let's start at 1 end at 5 and take steps of point 1 in between so that would create a whole bunch of numbers 1 . 0 1 . 1 1 way to all the way up to 5 . 0 and store them in the red called a vector the giant bunch numbers that you can operate on another way of creating a vector is using the linear space function on and a slightly different because basically say start at 1 end at 5 and give me 41 points so instead of specifying the increment you can say how many points you want in the vector notice when you type in these things if you type xaxis equaled 1 color not point 1 point 5 if you put the semicolon on the end you get a different behavior than if you leave it off right have those that anybody with 2 different but what I'm really depth but bright faint you semicolon of the semicolon basically suppresses the result of Matlab so for debugging if you leave the semicolon off you can see all the intermediate results of what's happening inside your simulation but if you put the semicolon in which you normally do for programs because shuts Matlab up so it stops reporting all that stuff it's useful to when you just typing at the command prompt to lead the semicolon often you can use it for debugging like for example of spiritual 1 1 of the X. axis as you can type in Linked xaxis and it'll show you the results illustrated 41 points or however many points are but you can just take xaxis all by itself and that's the way of printing out xaxis see if you put x axis semicolon would do anything but if you say X axis by itself it prints out the current result right to part now suppose that I don't want a uniform spacing like that you know point 1 point 2 . 3 all that but I just have a few values for the vector the way you do that in matlab is you can use the square bracket and a commaseparated list of values so I can set the xaxis to square bracket 1 . 0 comma 1 . 1 of 3 . 0 all the values that I want in the vector so they don't have to be uniformly spaced on you know it's common to do that but and then you can write simple functions once you've got a vector whether it's generated like we did a minute ago were like this so I can start to operate on I can say 2 times X axis minus 2 and that basically that's the equation for a straight line right so don't take all the values in X axis multiply that by 2 and subtracted and I'll get back a vector of Y values that matches the vector of exercise so that's basically a function you know like why equals F of X right but taken try and convince yourself that's what it does you could also say plot the xaxis come alive and generate a plot of the value but nascar useful as a going along you can look at the value is just by typing in Y and you'll see all the values of y or you can say plot xaxis come alive and you can do that interactively when you're at the Interactive Matlab at the active prompt now suppose I want to try y equals 3 times x axis times x x is plus 1 I'm trying to say y equals 3 x squared plus 1 right if you try that it actually fails and the reason it fails is because when the planet is trying to multiply these 2 vectors again start thinking in matrix notation if I take 2 vectors that are actually the same size and the same orientation and try to multiply them you can't do that in matrix notation right I need to take the white vector in a long vector and multiply them I need to do something more like the dot product if you're familiar with that and there's a notation for that in matlab syntax used dot star that star says multiply this vector by this vector don't create a matrix instead just take the 1st value here in the 1st value there and multiply them the 2nd 1 the 2nd 1 multiply them so goes value by value for the vector multiplies them instead of trying to create a matrix out of it right so there's a subtle difference here I want to make you aware of when you're dealing with vectors you also could have matrices and you get a start thinking in terms of matrices and vectors and and there's a reason that there is is some difference between star and started offending and which affect you want coach but but talk about matrices more because that's where Matlab really starts to get interesting when you start dealing with matrix matrix values and so suppose I define a matrix like that around 1 matrix like that the Matlab syntax for that is like this it's kind of like a vector that you can use semicolons to separate the rows so I can say a equals square bracket and and give the 1st row values semicolon the next row value semicolon the next row values so those those 2 things are equivalent and once I defined that matrix I can do operations on the transpose of a matrix and usually in mathematics to use like a prime symbols so if you do that in matlab or Octave if you type 18 it'll do the transpose of the matrix for you as a single command and it happens lightningfast right so normally in C language after a little function to do that but Matlab it's all built in superfast but but you could try this too if you took the matrix A is multiplied by a transpose and would basically could you do matrix multiplication on those 2 matrices and again when you're dealing with matrix multiplication the matrices have to have the right size they have to fit right in the the number of
10:21
columns in in the first one has to be the same as the number of rows in the 2nd 1 so that you can actually multiplied but uh and you can do the opposite you can take a transposed times a and you actually get a different result but you can do things like a transposed times a plus 1 but then what is happening the way I wrote it like this a transposed times a plus 1 basically adds 1 and every single value in the matrix but you might say well I really wanted more like the identity matrix I want to add that in when you start getting and all that stuff in my bringing up bad memories from like here early you 1st Purdue calculus courses something that the guys remember all of this matrices the identity matrix so that their their matlab has these builtin functions zeros ones and i so this zeros matrix when I say 0 3 you give me a 3 by 3 matrix full of zeros if I say willingness for it gives me a 4 by 4 matrix full of ones and if I say I 2 that gives me the identity matrix the identity matrix has ones along the diagonal and zeros everywhere else right and that's useful in math when you're building of the functions so so it's good to know about these functions if you don't believe me all the Matlab prompt and take these things and you'll see the results that you get that but so it could be that for my program to get the right effect what I really wanted to do is take a times a transpose and then adding the identity matrix which would only have ones on the diagonal the set of one's everywhere but to so now that we know a little bit about matrices we could do something a little more interesting so there are lots of other functions in Matlab on for example there's a function called I am read and I am really read image so I can give it the path for a J peg file and JPEG want and Matlab will read that up and then it will store it as a matrix will stored as a matrix that you can manipulate and pick apart and plot and do things with so we do that we set I am worried that function and we store the result is that I am but if I say figure and I am show it'll show me that image will pop up on the screen in this particular case what it is is a picture of the Matlab development team Simulink development team I guess and so if you're curious about the folks who wrote Simulink box there they are there in the distribution just grabbed a standard image becomes a matter of by weight is an equivalent and for octave but in earlier versions of octave earlier than 3 . 0 you having a separate function to do it announced built and so the Indian act is getting better and better closer and closer than that but now let's do something interesting with the image will take that matrix that we defined a 2nd ago I've got you know my matrix a which has minus 1 is in the 1st row of zeros and ones and there's a function in matlab called i and filter where you can take an image I am and you can take a matrix like a and you can convolve the 2 of them again and bring back all those bad memories we were last math class right convolution and basically takes that matrix at each pixel value it's gonna multiplied the surrounding pixel values and then give you a result and stored back on that pixel then go to the next pixel and I'll take that whole matrix multiplied by neighboring pixels that all up and then stored under the center again apply that little 3 by 3 matrix that we've got you apply it to every single pixel in the image and give a certain effect that's convolution than if you actually do this and then say I am show you get something interesting and of antibodies following along but I'll just show you the results so here is the convolution procedure basically that matrix it's applied to that to that through like 3 pixels and then stores the result on the center pixel and it does that for every single block in the image and what you get is something really weird get really weird image where all the the top lines are Albright and and yes I decided becomes all than other words what we did was we get something looks like edge detection in Photoshop period use calcium blurring only people use Photoshop calcium blurred edgedetection all the filters that you get a photoshopped this is how the implemented this is what photoshop does under the hood it takes a convolution matrix and applies it to your image and you can do the same thing level you get to create the effect it turns out if you use different images the different matrices here different effects so in this particular case my matrix was minus ones zeros and ones and that gave this vertical edge detection if I flipped it and had minus ones zeros in the middle and 1 is on the right side I would get kind of a horizontal edge detection and if I started messing with all the values not just using minus ones and ones but using weird values you start to get other things you calcium blurry goal all therefore affects you can find in Photoshop so now you guys have of a sovereign another summer jobs you can go off and create new new effects for Photoshop just by playing with Matlab and coming up with different matrices you can use larger matrices which will apply the effect out further in many neighboring pixels or smaller matrices you can play with all the different values and you get all kinds of different effects but that's how it works and there's a lot of matrix math in there are a lot of pixels in an image and each pixel has a lot of matrices that are being applied so that very good at that very good matrices but you can mess around and try different things to effect maybe only this is an exercise to all of you guys later if you mess around with and you take 8 times a transpose you get yet a different matrix and when you apply that as a filter you get a different effect which uh yeah I don't know I guess maybe if you if you are trying of matrices you'll you'll discover something amazing that the I guess the a and transpose is kind of a charcoal but which I do this for a screenshot for it kind of gives you more about this a vertical or horizontal it kind of applies it across the image and more diagonal fashion and by more
16:48
with matrices sometimes you don't want use the whole matrix sometimes you wanna manipulate individual parts of the matrix and there's a syntax for that so again suppose we have this matrix A and I wanna get at the very 1st element the 1st row and 1st column you can type a parentheses 1 call 1 and that gives you that 1st element should be minus 1 you can convince yourself if I say to common 1 should go to the 2nd row and the 1st column should 0 and so forth so you can pick apart the matrix by asking for different values within the matrix and addressing so in general that 1st index is always the row in the 2nd 1 the column and in Matlab they're always indexed from 1 C language is more like starting from zero this is from 1 by now matlab also has some other things where if I won the 1st world and 3 columns I can write argues that vector syntax that we had a minute ago the real looking at I can say 1 column 3 and it'll give me 3 columns but of that but in this case is a 3 by 3 so I'm basically giving the whole thing is also a way if you just want the whole world over the whole column is giving say colon so in this case I'm asking for role 1 and everything across all the columns the colon basically means from whatever to whatever everything so if you don't give specific indices it pulls the whole row of in this case but but you could also change values in the matrix you could say a to come 2 equals 3 and that'll just change that 1 element within the matrix in the 2nd row in the 2nd column of middle managers and then if you print out a little show and W. different value there so again is a mess around with all these matrices trying to come up with the ultimate photoshop effect this is how you put values in 1 at a time we can use that same kind matrix stuff to pick the image apart pixelbypixel so if you've got this image you can you can address the images if the were matrix you could say row 1 column 5 and will give you back the red green blue values in the image of that point actually gives you back a vector of red green and blue and you can say I want wrote 3 of all the columns are how big the images but wrote 3 whatever is there on the line is what I want and or you can subset the image you can say I want the 1st 100 rows in the 1st 100 columns no cut out of 100 by 100 image of the course so from powerful syntax to grab all this stuff but trash but now let's look at the plotting stuff if I did that if I said in my image what role 3 I want all the columns and I won't pick up the the 1st value red members it's going give me if you try this out we give you back red green and blue so the last 1 value there is red and 1 red but so what I'm going get is the 3rd world all the columns and the red component and I'll get that back as a vector called R. B. and then I can plot I want plot from 1 to the length of our be all the RD values and then I can give it are and will make it red if you try that you actually get a plot like this will show you the intensity of red values across that role of the image and I can also add some other stuff I can put a title on the plot I can put x label in y label so if you want to generate plots in Matlab and basically showing you how to do it right this is the plotting functions that's how you set labels and titles and that's how you what stuff but in that lab you could also say hold which means I like that figure just like it is and I want add something on to it and then I can get another vector like the green vector is the same thing except instead of getting the 1st component relative 2nd component green and then I can plot that and now it'll had now I've got the red plot in the green plot from the same plot and that's because I said hold so I got both but I can also add a legend on the plot to and we start doing that and I can mark the legend the corner that shows a sample of a as a simple green and labels but trash but here's a little 0 by the way CELF is the clear function clears up the plot so we have a hold on that will clear that and now I'm going to plot the next 1 and then I'm using a little bit fancier styles here I wanted that dotted lines and circles all that kind of stuff so with that funny string are colon old that basically the Army's red the color means the dotted line in the 0 means but circles at all points so Matlab has this really weird cryptic notation for the plotting and use your cheat sheet if you ever want to generate a matlab plot you can use the use of single letters for all the colors you can use those symbols to get dashed lines and solid lines and that national that and then if you wanna mark the points you can put pluses and stars and axes and all that weird stuff and so you don't have to use that stuff but if you want to dress up your clock that's how you dress up the style but so you got the basic syntax were almost done but I wanna show you what it looks like when you start writing Matlab programs you write a program is starting in the functions in language syntax and all that so here is an example of a function that I called edge matrix of edge matrix is that matrix that we had a minute ago the minus 1 is the zeros the ones I and I want to be able to generate I have a function here I can pass in any size and I could create a 10 by 10 edge matrix and it would generate this giant edge detection matrix for me I oriented Peoria either wars for words on orbit for vertical so like I can now call this edge matrix function passing any size any orientation and edge matrix will generate a matrix for me
23:06
that has always minus ones and all that stuff and I could use that then whether writing my image filters recreating photoshop I can call my function in filter with my image but instead of hard coding matrix A here I can call edge matrix here I'm getting a 5 by 5 matrix and oriented horizontally or I could pass in 7 and work or whatever so I can pass and whatever values I want this will generate an edge matrix just like our matrix a and then applied as a convolution so this is what it looks like to define a function in Matlab and the college in Matlab looks a lot like other programming languages and promising but true but by now like other programming languages Matlab can let you do something so there's a for loop and here I have 2 nested for loops you can say for i equals and then we use that vector syntax 1 Cohen and basically generates a vector from 1 day and then then it looks through all those values so this is a writer looping in Matlab and the same thing for j so I've got the outer loop i which is going through all the rows and and the inner loop j which is going through all the columns of 2 1 day and is and with the size of my matrix so that's how you write for loops and this is what you can think of in your mind and trying to build this fivebyfive detection matrix and looping over all the values I and I'm looking over all the values J and figuring out all the values now inside the code I have a little if statement to see what the orientation is if its horizontal that they use 1 formula for each value and if it's vertical many slightly different that is me different values him over the edge detection matrix if it's on horizontal and horizontal I think it's like this and this vertical it's sort of the transpose of it all all the minus values are here and 0 and the class values on the other side of kind of a transpose so by if statement if you wanna write if you mean that is a if give it some kind of of the condition that you can check and then you have some code else and then some of the coding and and see again probably looks a lot like other programming languages like C or Fortran or something like that but I you write an if statement the details of this function arts Importanc unjust right show you language syntax and get used to it if you have to write some code but with this formula set up if I pass in the value cost for horizontal all end up with a matrix that looks like that and if I pass in anything else Burke probably is what I should pass and I'll get back so the way this is set up that is producing the ones and zeros in the right orientation based on the if statement OK so that kind of shows you a lot of the syntax basically have to declare function you know you can see some variable assignments for loops and if statements that's basically how Matlab works discounted Overview trap the trouble with this example is that if you actually run this example you write this example you start running it's terrible it runs really slow and it's a horrible implementation and I hope you guys any of you already know matlab a already laughing at me thinking why would you write that way you wouldn't crazy so we don't do this as a good example to show you all of the stuff in Matlab but when you when you program in matlab you actually program a different way to think about the programming differently turns out if I were writing a Clanguage program or Fortran program what I wrote is good but when you're programming matlab you have to take advantage of the vectors and matrices and you really don't do that if you program the same example in Python on tickle again you do it differently it turns out each language so it has a way of doing way of approaching the problem in that lab that wasn't the way to approach the problem if you're really gonna write this function in matlab you write something that's a whole lot more cryptic but a whole lot faster that's always the trade off right so the way this function works on my short version basically generates a matrix of ones and J. and creates a vector from minus half to half the size of my matrix so fun trying to generate a fivebyfive it goes from minus to to to those of the values I think I have a little baby here to help you understand it so what I'm doing is I'm generating up this vector minus half to half and multiplying by a matrix of or a vector of ones I guess just 1 row out the ones matrix and if I multiply those 2 vectors i and up with what I want and except for the case where 1 oriented horizontally than any the transpose right so by using matrix multiplication I can avoid all those for loops the for loop so what kill you in matlab if you're gonna stand for i equals 1 to a million and then inside that for j equals 1 0 million it's going to take you a million days to get your job done so don't do that what you do instead is you come up with a way of figuring out vectors you take two vectors multiplied together and you get the result at lightning speed and then you say what's close by what the transpose well you can get that lightning speed to you just say and equals transposed phone done so the lesson to take away from all of this is 1st of all there's a lot of syntax in Matlab that's a lot like what you see in other languages and 2nd of all don't use matlab like you use other languages and learn how to program with these vectors and these these matrices and you end up with programs that are much better than than the brute force its astonishing I'll tell you I've seen grad students write code and they run this program and it takes like an old 7 hours to run the program and this they're trying to figure out how they can get the program of anterograde Condor and what's the biggest supercomputer that I could use to run another program and all you have to do is rewrite the code like this and your program will run in 35 seconds that every program but there are a lot of bad code out there that takes too long to run simply because the way it's written so pay attention to the way codes written if it's taking too long to run and you can make a run a lot faster against to by 1
29:25
last thing I wanna show you if you're writing a Matlab program you might have some simple inputs and simple outputs and simple file stuff again just to show you had a quick overview Matlab just so you've seen it once you have some some examples to copy all of this is a very simple program had props for different things and so there a test function which will display a product so desk who are you will print out who are you and then the next step input lets you enter something it says enter here it your name and then that acid the underlying says I'm going give string input so enter your name give me a string and I'll come back as a variable called names the next line says enter your age but you notice there's no s there so matlab by default will asking for numbers so if you type in you know 37 then H be 37 actually the way this is written I think you could type in 37 . 2 and whether that's about the future but anyway and you can type in any number for your agent will take that the so that's how you basically part using this and using input but when you go running but it'll actually look like this so you if you run this program will say who are you enter your name you can take that in an area aging can type that in and then the printout hello Michael and so on and so forth so if you look at the S. print out functions and if you're familiar with the language at all C language has this printer function formatted print it basically builds a string in printed out the aspirin version builds a string but doesn't print it out it just gives you the string part and so see language as as for enough in a lot of other languages due to just Matlab is 1 of among so if you wanna build a string a nice way to build it is by using a spread out and you can say hello per cent s per cent s becomes the place where it puts the string so the name that I didn't hear that name argument it's substituted in place of per cent s inference hello Michael whatever the name was here a little like like that and this is the way you actually print and think there's a printer function to open anyway just as a way of bringing string on the bottom part for enter a file name you can again use the input function to prompt the person for a file name and then like C language on Matlab has F open f print and closes the file function F open opens the file and the W means for right after printer prints out does like the aspirin except for a file it prints out to the file ideally you opened FIDE it builds a string % is % the years old so the name substituted in % s and the age substituted in per cent the per cent D is a decimal number integer but so it builds that string and then writes it out to the file and then after close closes the file so just so you see that once you can tell your advisor yes I've seen file I O in Matlab hopefully you never have to do it again but that's an example right there is a
32:38
lot more stuff about Matlab it you can learn including great tutorials that that works that you can look up and there's also some toxin nail of including this really great talk of by this guy from Intel of Matlab do's and don't so what you want to learn more about matlab there's a lot more that you can go into that which is kind of a quick flyby to get you guys to point see it wants to so I wanna
33:02
give you achieve its no time mess with Matlab then you can do that in a workspace by bringing up matlab you can do that in a workspace for bringing up active or you can use active you whichever way you want but what we're gonna do is of type in some code and try out the plot it turns out that you can you guys ever use fire graph when you're a kid as that my dating myself up OK nobody else that's at age they are but so back in the back in the 18th century when people were looking for funding there was no youtube anode Facebook and there was a system of gears that you could pin down on a piece of paper and you put the gears in with a pen in a move that depend the gears around in a circle and would generate something looks like that and depending on which year used in which holders that depending on you would get different patterns so basically you stick your pin you move it around it would create this curly loopy kind of pattern and they take another during different color pens and you put it in a move it around and we create another pattern on top it was great fun again back in the days before the added fantastic this is what people do also we sat around campfires and showed a drawings to 1 another so what we did not so there's a mathematical theory behind this and you can use complex exponentials to generate the same kind of effect spiral graph so you know you need to the complex number i times 2 pi times some number and 1 times the if you add that to EDI 2 pi times a different and 2 times t and use the same thing again with n 3 you pick 3 weird values for 1 and n 2 and 3 you basically get a spiral it's like the gears that the complex numbers of the gears basically oscillating around a different random weird frequencies and they give you the strangelooking plots so if you take the real part of the vector that you produce and the an imaginary part you plot them you get spiral that if you're interested in the theory and what life was back like in the 18 hundreds and then go ahead and take a look at the you can follow the article the link to the Linux because that it will explain to you the whole thing about spiral graphs and all that but if you just want to try it out in your workspace fire matlab or Octave and type in this cold and get some plots or you can try not to do to go and give that a shot trial are that solution that I came up with a for this lab assignment but
35:40
does do it on the fly that
35:43
so I'll go to 1st of all good Hobbes and we can find
35:48
that to all active you so here's
35:52
this active you tool and you
35:55
can see some screenshots and contemplate through the video conditions it works and all that but but anyway this is an
36:04
easy way to launch stuff have to log in that fires up like
36:09
any other tool OK so I can
36:16
type whatever I what I'll start with t equals linear space 0 0 2 1 1 thousand and if I just do that much and click Run script it'll show me OK there's my vector t right and on the Council of but I can see like all of the values and that's because I didn't have a semicolon at the end here so showing me all the intermediate results source stick semicolon there and I'll keep going up and I'll say i z equals x and y times 2 times pi times and 1 times t plus x of y times 2 times 2 pi times and 2 times t plus the last 1 by times 2 times pi times and 3 times t the I and the pi ability and in that lab so it knows about those and then I can plot the real of z versus the manager of z by right now I can type script and called it gives me an error it says anyone's undefined right active use kind of good for that to because that helps you track the years in a script and figure things out anyone's undefined because I didn't define it from what values manganese for this plot was trying set if you set and 1 equals 23 what was the 1 I liked negative 19 38 I forget try that so those are 3 values and then the rest of my code and I can't click Run Script it'll go off and run pay that kind of good 1 and so that's the plot that I asked for there's also a debug tab here that shows me that the debugging information I can look at the variable t I can look the variables z and I can I see the other variables to 1 and 2 and 3 women just ordinary values on and I can try different I I can also see the Council output here and review all of that and again I missed a semicolon somewhere because I'm doing a lot of output and the consul is probably the end the z lined up on just put a semicolon there'll be be nearly as much output and I can try some different cases I can play with them you know Sue happens it's minus 12 and run it again and I get a different patterns and so forth so active you as a kind of a good quick and dirty way to write a little bit of matlab code that's not very good for writing really complex programs of lots of functions and so every good debugging the real Matlab program better than that all work on that and that kind of thing where you can get into the actual active you on the outside the actual active program workspace so show you
39:00
that require that's going in and 2 here but below program here called Spiro died and so here's my little matlab scripts comments in a Matlab script user % sign for the comments are defined my and 1 million 2 million 3 I set my space may seem plot all that stuff I showed you a minute ago I store that down a script file called Spiro so let's run that the way you run that is 1st of all I have to make sure using active so yup there is active it's got a start next to it so I'm all set up I can't active it fires up and
39:42
then I can say spiral but and what it'll do is it'll look for the file called spiral and in the current path or whatever my search path is for proactive and then it loads that file and and runs it in a course like my last for a plot so 1 and plot it down so I can edit the file and run it again and you know this is the way you you build a program by probably if you were trying to do more of a script and debug it you want saving the file you want to try it out of the interactive command prompt is good for debugging to because I could say assumes going wrong I can go I know what's going on what's z 0 that's see what's t 0 that's the right so you can just typed commands interactively and mess around and I could say well what's the plot of z versus T and it'll plot that up forming and that's not aspire graph but anyway that's the versus the so you can you can do lots of things interactively on the command line encountered tobogganing and figure out your program that way but