A Quick Introduction to Matlab

Jodi Christiansen, Department of Physics, CalPoly, San Luis Obispo, CA

(version: April, 2008)

Matlab Environment: When you fire up Matlab it provides you with a Graphical User Interface (GUI) designed to help you do technical calculations. The Command Window is the place you will type in the commands you want executed. A variety of other windows are also displayed and eventually you’ll find yourself using their information, or customizing the windows that you want displayed. When you save your work, it will put the files in the folder (directory) indicated on the top bar.

Make a folder on your flash drive to hold all your work. Switch to this folder in the top bar so that your files will travel with you and be accessible from other machines.

As with any programming language, the syntax looks like Greek when you first see it and must be learned over time. In the text that follows, commands in red can be cut and pasted to the command window just as you might cut and paste them into Excel or Word. Try it out below.

Get a Clean Start: If you’ve been working for awhile and need to clean out all the memory and plots to start over type the following in the Command Window.

clear all; close all;

Arrays: Matlab is based on processing arrays. Calculations will be very inefficient to calculate equations if you don’t use the array syntax. Because it’s central to the design of the language, it’s really important to get proficient at manipulating them. A basic one-dimensional vector is created with the syntax x = startValue:stepSize:endValue;

x = 1:1:10;

Try pasting this into the command line with and without the semicolon at the end.

x = 1:1:10

In Matlab any statement without a final “;” will print its value to the screen. There is no need to add print statements…just add the semicolons when you get tired of seeing all the output. It’s very handy for debugging to remove a semicolon wherever you’re in doubt about what’s going on.

It’s also possible to make arrays with the syntax:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

but this takes more typing than is needed.

A couple of matrix utilities will come in handy. To make a matrix with all values equal to zero, use the zeros(Nrows,Ncols) command, where Nrows is the number of rows and Ncols is the number of columns. And to make an array with all values equal to one, use the one(Nrows,Ncols) command.

myzero = zeros(5,5)

myone = ones(2,3)

And, to make an array filled with the same constant number, use the ones(Nrows,Ncols) command.

gravity = -9.8*ones(5,5)

The size() function returns an array [nrows, ncols] with the number of elements in each dimension of the array.

[nrows,ncols] = size(gravity)

Comments: Comments can be added anywhere in Matlab code by putting a % in front of the text.

x = 1:1:10 % this is an array with range 1-10.

In these files I’ve turned comments green but that’s not a Matlab requirement. Both the red and green can be cut and pasted and Matlab will ignore the part in green.

Help: There are lots of ways to find out about Matlab commands. The Help button on the toolbar is useful, but sometimes it’s useful to type help on the command line.

help plot;

This displays everything about the plot command with examples that you can cut and paste into your command window. At the bottom of the help information there are links to other functions that are related to plot, so you can often find just what you’re looking for. . There is also a search feature from the Matlab Help menu.

Functions: There are lots of built-in functions:

help elfun

help specfun

As you need more functions, use help to find the others.

Here’s how you’d use the various functions:

y1 = sin(x);

y2 = log(x); % this is the natural log

y3 = log10(x); % this is the log base 10

y4 = besselj(1,x);

Lots of times I make my own functions. Here’s how to write y = x5

y = x.^5

The ^ symbol is used to denote an exponent. The period tells Matlab to do this math on every element of x. Without the period Matlab tries to treat the x array as a matrix and do matrix algebra. There’s more on this in M1_Arrays.

Plotting: To plot the functions above as y vs x, cut and paste the following to the Command Window one line at a time.

plot(x,y)

plot(x,y2,x,y3) % plot two functions at once

plot(x,90000./x.^2) % plot a function w/o precalculating

And all plots have nice labels

title('V(x)')

xlabel('V (volts)')

ylabel('dist (meters)')

You’ll want to change the axis limits occasionally with axis

axis([1 5 -1e5 1e5])

Numbers:

Here’s how you enter numbers in scientific notation.

K = 8.99e9

Pi should never be entered yourself as 3.14159… you’ll make a deadly typo someday. Please use “pi”. It will never have a typo and is far more accurate.

pi

This prints only the first 5 digits to the screen, but there are many more digits in the computer’s memory that will be used in calculations.

Continuation Lines: If a line is too long you’ll want to continue it on the next line. Matlab understands that “...” means that you’re not done with the line. Here’s an example.

y = cos(5) + sin(5) + tan(0.1) ...

+ 1000 - acos(0.5)

Etc:

There’s much more, but it can wait for later.

Assignment: M0_Intro

1. Make an array of values x = [0, 0.02, 0.04, …, 3.0], i.e. 0<x<3.0 with a step size of 0.02. Test it by comparing the expected and actual values given by size(x) and x(1:7). Explain what is returned by x(1), x(7), x(end), x(:). Plot the sin of (x multiplied by p), i.e. sin(px) vs px. Label the axes.

2. Make an array, 2<x<5 and with 20 elements in the array.

(i.e. size(x) should return 1 20).

Reports: In the report for your assignment, please include the following.

·  Analysis: Give me some indication of the physics and numerical approach.

·  Tests: Tell me how you know your program works.

·  Results: After the program works, you can include the answers to questions and additional plots with your interpretation of their meaning.

This week the analysis is minimal, but in the future you’ll need this section to talk about the physics and how Matlab is used in the solution.

Page 2