0% found this document useful (0 votes)
3 views25 pages

Matlab Programming Module5

The document outlines exercises for programming in MATLAB, focusing on creating scripts and functions to compute sine and cosine series. It includes detailed steps for writing a script file, converting it into a function, and implementing error handling and optional outputs. The exercises also cover advanced topics such as passing functions as inputs and using recursion.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

Matlab Programming Module5

The document outlines exercises for programming in MATLAB, focusing on creating scripts and functions to compute sine and cosine series. It includes detailed steps for writing a script file, converting it into a function, and implementing error handling and optional outputs. The exercises also cover advanced topics such as passing functions as inputs and using recursion.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

am

M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
M
at
la
b
pr
og
ram
m
in
g
m
od
ul
e5
Programming in MATLAB: Scripts and
132
Funcio%
EXERCISES

a script file named


1. A script file to compute sine series: Write
that computes the value of sin(a) at a given using n terns of the Sineseries m
series
of the sine function: expans\u

e5
n

sin(z) =I - t5! (2k - 1)!


k=l

ul
Follow the steps given here.
" First, query MATLAB to see if the nane sincseries is already taken by

od
variable or function with the command exist ( 'sineseries') What some
MATLAB response mean? Hint, see on-line help on exist.]
does the
" Include the following line as the header (HI line) of your script fle

m
%SINESERIES: conputes sin (x) from series expansion
Now code the formula so that it computes the sum of the series for a
m
scalar r and a given integer n.

g
" Save the file. Type help sineseries to see if
MAlLAB can access your scrirt
file. Now, compute sin(T/6) with n = 1, 5, 10, and 20. Compare the
result
in
Do the same for some other r of your choice.
m
2. A function file to compute sine series: Take
the script file written in Exercise 1
and convert it into a function file using the following steps.
am

" Name the function sine_series and modify the


" Let
H1 line appropriately.
and n be the input to your function and y (the
" Save the function and execute it to
sum) be the output.
see that it works and gives the same output
as the script file in Exercise 1.
r

" Modify the function to include more


og

on-line help on how to run the function


" Modify the function so that it
can accept a vector r and give an
y. appropriate
" Modify the function to include a check
pr

on the
proceed only if n >0 is an integer, otherwise it input n. The function shoud
" Provide for an optionaloutput err, should display an error mesage
to sin (x). Hint: use conditional which gives the % error iny when compared
b

" Modify the function so that it


statement on nargout for optional output
takes a default value of n = 10 if the user
la

not specify n. Hint: use doe


" Execute the function to nargin.
check all features you have
added.
at

3. Afunction as an input
to
function can be passed in the another function: There are several wayssin which a
M

to be used in the input list input argument list of another function. The function
can be written as an inline
page 83) or it can be coded
in a function (see Secttion 3.51.
input list depends on how it function file. How the function is passed in the
is coucd.
Code the function y(r) =
sint as an inline
sincfun.m. You will use this function sinc and in afunction filecalled
page 93) in various ways in function in the input list of ezplot 38
(see Section
the following
" Use the inline instructions.
function
Over the default domain. sinc in the input list of function

ezplot to plot the


Publishing Reports

133

function sincfun as Astring


in the input list
domain. of ezplot to plot
over the default the
function

. Create a function handle for sincfun and use the handle in the input list of
to plot
the function over the default domain.
ezplot

e5
In this exercise you will
wTite a simple
siníz) and or cos(r) from series expansion using two Subfunctionsfunction
s u b f u n c t i o n s :

Write
to fnd
sine_series
4

ul
And cosine_ series.

Write a function named cosine_series to evaluate the cosine series (follow

od
Exercise 2)

p2(*-1)

m
cos(z) = 1 4
k=l
2(k - 1)!

g
and test
the function to make sure it works correctly.
Execute
named trigseries as follows.
new function
in
. Write a
list of the function should include r, n, and another string
- The input The user can specify 'sin','cos', or
both' for optiona
m
variable options. include u and err
default should be both'. The output list should
The
am

Exercise 2.
as discussed in should call sine_series and
cosine_series s
trigseries specifes
The function depending on what the user
y And ert,
appropriate to compute
suitch using options as
this call sequence with
r

Implernent
in options.
og

the user asks


the switch.
and err to be twO column arrays if
output y
- Program the default.
'both' in options
or as the subfunctions
for as
pr

cosine_series
sine_series and
functions
Copy and paste the trigseries in the sane file.
CoSine_series2.m or
rename them
below the function and sure
and options. Make
sine_series2.m
files
b

"Delete the original trigseries with various input x=[0 pi/6


Something else. Test trigseries with
only one input
la

execute
correctly. Now answers?
it works you get
reasonable
pi/2]. Do
p1/4 pi/3
at

Exercise 4,
developed in
function
trigseries,
the only input.
5, Profile a function: Profle the 0and as
M

between
spaced points
a vector z of 100 equally
taking this is
(Note that
recursion.
Compute n! using conceptually a recursive
6. Recursion: Write a function to n!.
However, it is
MATLAB.)
recursion in
Compute
not the most eficient way to test
and
calculation that is easy to implenent

You might also like