lecture2 - Lecture 2 Matlab Programs In Matlab , programs...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 2 Matlab Programs In Matlab , programs may be written and saved in files with a suffix .m called M-files . There are two types of M-file programs: functions and scripts . Function Programs Begin by clicking on the new document icon in the top left of the Matlab window (it looks like an empty sheet of paper). In the document window type the following: function y = myfunc(x) y = 2*x.^2 - 3*x + 1; Save this file as: myfunc.m in your working directory. This file can now be used in the command window just like any predefined Matlab function; in the command window enter: > x = -2:.1:2; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Produces a vector of x values. > y = myfunc(x); . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Produces a vector of y values. > plot(x,y) Note that the fact we used x and y in both the function program and in the command window was just a coincidence. In fact, it is the name of the file myfunc.m that actually mattered, not what anything in it was called. We could just as well have made the function function nonsense = yourfunc(inputvector) nonsense = 2*inputvector.^2 - 3*inputvector + 1;nonsense = 2*inputvector....
View Full Document

This note was uploaded on 02/09/2012 for the course MATH 344 taught by Professor Young,t during the Fall '08 term at Ohio University- Athens.

Page1 / 3

lecture2 - Lecture 2 Matlab Programs In Matlab , programs...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online