240 likes | 460 Views
Introduction to Matlab. Can be programmed just like Fortran Adds visualization support Kinda like your graphing calculator Adds Matrix/Array support. Variables & Data Types. Unlike Fortran there are NO data types to be declared
E N D
Introduction to Matlab • Can be programmed just like Fortran • Adds visualization support • Kinda like your graphing calculator • Adds Matrix/Array support
Variables & Data Types • Unlike Fortran there are NO data types to be declared • Variables can support Real, Integer, String, and Imaginary numbers • To create a variable, simply use it
Simple Examples %Example1.m X=5; Y=3; Z=3X^2+5; Z % character denotes the start of a comment lines that end with a “;” are simply run lines that do not end with a “;” are run and results displayed on command window
.m Files • Once your program has been saved as a .m file it can be run by either pressing F5 in the editor or by typing the file name in the command window • Other .m files maybe be run from within different .m files
Working with Matrices/Arrays • Matlab is made to deal with Matrices and Arrays much simpler than Fortran • Variable(X,Y,Z) can be used to access each element • Just as in Fortran Loops can be used to perform functions
Loop Example %Example2.m - Loop for i=1:40 x(i)=i; y(i)=i^2; end plot(x,y); plot(x,y) creates a 2D plot and displays it
An Easier Way • When dealing with large amounts of data, loops in Matlab can be very inefficient • Matlab allows mathematical operations to be performed directly on a entire array or matrix (This is called a vector operation as opposed to a scalar operation)
Modified Loop Example %Example3.m - Loop x=linspace(1,40,40); %Linspace creates an array 1 to 40 of 40 elements y=x.^2; plot(x,y); In Matlab matrix/array math .^ .* and ./ performs those operations on a element by element basis While ^ * / when performed on a matrix will attempt to perform matrix math
Matrix vs Element Math Element Math Matrix Math
Circuit Example 10 Ohms + 10V - 3uF 0.01 H I=V/R=10 /
RLC Series .m File %Example4.m - RLC w=linspace(1,40000,1000); i=10./(10+(1./(j.*w.*3.*10^-6))+(j.*w.*0.01)); plot(w,abs(v)); j is intrinsically unless its redefined
10 Ohms 1A 9uF 0.08 H Assignment #1 1. Write a matlab program to determine the frequency response of the voltage across this RLC circuit 2. Provide Magnitude and Phase plots of this circuit (Remember to label graphs) 3. Bonus: Explore usage of subplot commands to do multiple plots on the same graph