Practical 5: Function and Script M-files

Script M-file

Creating M-file

 

Exercise

> The following script M-file finds the value of the function at y= sin x + x3 at x = 3.

% exercise1script.m

x = 3

y = sin(x) + x^3

> Run this M-file by typing the following in the Command Window:

exercise1script

> Then update the M-file to find the value of f(x) for x = 4, 5, 6.

Quite often we need to be able to calculate the value of a function y=f(x) for any value of x. Obviously, it is not practical to change value of x each time. For this purpose MATLAB has a special type of M-file, called M-function.