联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Matlab编程Matlab编程

日期:2022-11-17 10:26

ELEC1703 – Algorithms and

Numerical Mathematics

Function program; work with Arrays and Matrices in MATLAB – Assignment 1

School of Electronic &

Electrical Engineering

FACULTY OF ENGINEERING

ELEC1703 – Algorithms and Numerical Mathematics

Function program; work with Arrays and Matrices in MATLAB – Assignment 1

In ELEC1702 MATLAB Computer Lab sessions you have gained basic knowledge about MATLAB platform,

MATLAB syntaxes, flow charts, how to plot a function, looping, one dimensional arrays, how to make for or

while loop and use if-elseif ladder in simple algorithm. Now, in ELEC1703 we will move further, using

MATLAB to design simple MATLAB codes to be used for basic algorithms in numerical mathematics. In

ELEC1703 Assignment 1 we will introduce Function subroutine and further discuss Arrays in MATLAB this

time 2-dimensional arrays - matrixes.

Report format

This assignment is split into 3 parts, questions are shaded blue, while examples (if provided) are shaded grey.

- Answer all the questions in separate document (your report), include your name and student number.

- Make your report as organized as you can (e.g use tables for short questions, different colours, fonts

etc.). Assignment report template is provided on Minerva.

- For questions where you do something in MATLAB command line you must copy-paste input/output

from MATLAB to your report – you can do this directly (copy-paste) or take a print screen and paste

a picture.

- For questions which require you to write a code you should take a photo (snipping tool or print screen)

of your code in the report (or copy paste it) and also upload corresponding .m file along your report.

Also add comments into codes (using MATLAB symbol % ) and explain what lines in your code do in

the report.

- You should also add comments explaining what you did and notify anything you found peculiar in

MATLAB (and give an opinion why that happened).

- Only parts shaded in blue in this assignment need to be answered in your report. Grey shadings are

simply examples for you to familiarize with a topic.

- 5% of this assignment is the organization, make sure all .m files are uploaded on the Minerva, named

sensibly and cross-referenced in the report. Using the provided report template is recommended.

Contents

Function Files..........................................................................................................................................................3

Question 1 (20 marks).........................................................................................................................................4

Question 2 (40 marks).........................................................................................................................................6

Matrices in Matlab..................................................................................................................................................7

Inputting matrices................................................................................................................................................7

Matrix operations................................................................................................................................................8

Matrix types.........................................................................................................................................................9

Question 3. (35 marks)........................................................................................................................................9

Page 2 of 10

Dr D Indjin and Dr A. Demic

ELEC1703 – Algorithms and Numerical Mathematics

Function program; work with Arrays and Matrices in MATLAB – Assignment 1

Function Files

Functions are useful for complicated program to be broken down into smaller parts. Also If series of

statements is to be used many times

Syntax:

function [output variables]=function_name(input variables)

Statements describing function

end


Function file name is to be saved as function_name.m and the first executable statement must be “function”.

If there is more than one output value, one needs to put output variables in [] bracket. If there is only one

output variable the brackets are not necessary. Do not use input command within a function, since function

arguments are its inputs. Matlab does not have specifiers of data types for its variables, therefor if your

function operates with whole numbers (integers) you need to provide protection of the function in it.

Variables defined and manipulated inside the function subroutine are local to the function.

Please use Matlab help function in the command window to read more about syntaxes.

Example: Function for sum of n integer positive numbers

function mysum=my_sum_fun(n)

if (n<0)

error('Only positive input is accepted!')

end

if floor(n)~=n

error('Only integer values are accepted')

end

if numel(n)>1

error('Only single integer values are accepted, array/matrix input is prohibited')

end

mysum=0;

for i=1:n

mysum=mysum+i;

end

end

The first two if statements protect the function from negative and decimal input, and the third if statement

protects the function from an array/matrix input. Since Matlab does not have data type specifiers, unskilled

user may attempt sending illogical input to the function. Note that this can create a lot of execution problems,

for instance, for (or while) loop would not run correctly if n was an array or a matrix, however you will not get

an error message! The first iteration when i=1 will execute as mysum=mysum+1; and output of this function

will be mysum=1 with no error messages from Matlab! As a creator of the function you need to be very careful

and predict when you actually want your input to be an array and when not. Matlab has “dot” operators as ./

and .* to deal with the arrays which is useful when dealing with mathematical expressions, however if you

forget the dot, illogical output may occur with no errors on the screen! Generally “dot” operations simply save

you from writing a for loop that iterates through the array and performs * or / operation on each element.

Make sure you fully understand the usage of these operators.

Page 3 of 10

Dr D Indjin and Dr A. Demic

ELEC1703 – Algorithms and Numerical Mathematics

Function program; work with Arrays and Matrices in MATLAB – Assignment 1

Question 1 (20 marks)

1.1. Write down MATLAB function subroutines which defines the following functions (please make

separate .m file for each function):

f 1 ( x )=2 (sin x )

cos x

x

f 2 ( x )=e

xsin ?(x)

Write then the main MATLAB program, which evaluates functions for x between -2? and 2?,

and plot the functions.

(10 marks)

1.2. If you check the MATLAB help page on plot function, you’ll discover numerous ways how to

set up your graphs by typing code. If you learn how to do this once, you can then copy paste

the set-up code for every future use when you need nicer graphs for your report (the default

behaviour of MATLAB’s plot function has very thin lines and very small font for example).

A better approach is to write a custom function that plots your data and contains such copy

paste code.

Construct a function that extends MATLAB’s plot function. The function needs to have at

least following arguments:

function z=better_plot(x,y,LineSpec,LineThickness,xlab,ylab,FontSize,enablegrid)

where x and y are data arrays that you wish to plot.

LineSpec is a string defining colour, marker and line type (same as in ordinary plot function)

LineThickness is a positive integer number - thickness of the data line you wish

xlab is a string describing the label of your x axis

ylab is a string describing the label of your y axis

FontSize is the size of font you wish to use for your axes

Enablegrid is a logical value that disables grid (0) or enables it (any numeric value that is not 0)

Test this function on data of your choice.

Note: Since MATLAB is defaulting data types, it is relatively difficult to validate if you user is really

sending input as string (word in single quotes as ‘word’. For simplicity, assume all input is valid, so

you do not need to worry about this, however do pay attention when testing the function, all string

arguments in this function need to be provided in single quotes and LineSpec variable needs to follow

syntax that MATLAB’s own LineSpec properties follow (check help page on plot function).

MATLAB is also not allowing to leave out a function argument (setting a default value behaviour is

complicated in MATLAB in comparison to C/C++ programming language), so even when you do not

wish to set some variable when using the function above, you still need to send empty string as the

argument (i.e empty quotes ‘ ’ )

(10 marks)

Page 4 of 10

Dr D Indjin and Dr A. Demic

ELEC1703 – Algorithms and Numerical Mathematics

Function program; work with Arrays and Matrices in MATLAB – Assignment 1

Example of how to use the function better_plot (that you need to create):

x=linspace(0,2*pi);

y=sin(x);

figure (1)

better_plot(x,y,'',5,'x','y',24,1)

figure(2)

better_plot(x,y,'-*r',7,'x','sin(x)',18,0)

Code output:

Figure 1: Figure 2:

Notice how all settings got applied, if you do not want some setting to apply, just pass empty string to

the function (as is was done for LineSpec for figure 1.

All settings for figure 2 got applied as instructed – Line spec is saying graph line should be red full

line with * as data markers, ylabel is saying sin(x), there is no grid, and font is smaller.

In both examples, this is still low plot quality as text on axes is hardly readable and data lines are too

thin, however depending on the size of figures you wish to place in the report, you can easily tweak

this function so it generates great figures. You can also add more plot settings, as setting text on

axes to be bold etc.

Note that if you wish some setting to always be applied and not controlled as function argument, you

can do that. Just do not pass as an argument, and define it in the function itself as all variables you

create in the function files are local (visible only in that function file). Also never name a variable in

your function the same as some variable that is passed as the function argument, because then you

would be overwriting the function argument.

In this way you can customize plot function settings however you like or need and this is very useful

in future whenever you need to create graphs for your reports!

Page 5 of 10


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp