Вы находитесь на странице: 1из 5

MM419, 18-08-2015

MATLAB INTRODUCTION
MATLAB interface

The central panel is the Command window where calculations happen. Top
right panel shows the variables in the workspace and the left column
shows the files in the current folder.If you see a different interface,
go to Desktop -> Desktop Layout and select Default.

Most operations in MATLAB are identical to Octave. Help regarding


details (including syntax) of a function can be obtained by typing help
functionname in the command window

Problems:
1. Solve a system of equations in the command window
2x+3y=5
x+2y=7
To do this, write the left and right hand sides of the equation as
matrix vector operations and solve for the unknown. Using both matrix
inverse (function inv) and division operation (AX = B, can be solved
using A/B)

2. Write a script that does the following:


Create a matrix A = [1 5 9
2 8 7
5 3 4]
(a) Calculate the transpose and inverse of A.
(b) Now create another matrix B = [4 6 1
3 7 5
2 9 3]
Calculate C = sum of 1st row of A and last row of B.
(c) Create a vector V = elements in the first row of A and vector U =
elements in the first column of B. Calculate the dot and cross
products of V and U.
(d) Vectors
and hold on
for quiver3
calling the

can be plotted by using the quiver command. Use quiver3


to superimpose plots of UXV and VXU. Note: Read the help
carefully. Specially notice the inputs to be given when
function.

(e) If A*X = B(:,2), calculate X. This can be done either by using


inverse of A or by dividing B by A. Check that the two calculations
give you the same result.
3. Write a script to convert from C to F: C = 5*(F-32)/9.
Temperature will be taken as input from the keyboard by using the
command input.
4. Write a script to calculate sin(x) using following formula:
k 1
n
x 2 k 1
sin (x) = (1)
for k=1 to n
(2k 1)
k 1
for x= 0 to pi in 20 steps, evaluate the function for n=10 and plot
the function.
(Note: The denominator is factorial of (2k-1)

Also plot the in-built sin (x) function on the same plot and
compare. Remember to label the axes and to insert a legend showing
which plot is the series summation, and which has been calculated
using sin(x)
5. Write a function to compute the sum of a geometric series
1+r+r2+r3+rn for given r and n. So the input to the function
should be values of r and n.
The difference between a script and a function is that in a script,
all information (data) needed to do a calculation is part of the
program itself. Of course the input used above is an exception.
A function is written in a way that input can be given when the
function is used. This input is used to do some calculation (or
make a plot) and the results are returned. The input can be
numbers, or arrays.
As an example, see the following function that takes as input a
vector consisting of numbers whose average is to be calculated. The
output of the function is the average value.
EXAMPLE of a function that calculates the mean (average) of some
numbers
function [meanval] = meancalc(h)
%meancalc Calculates the mean of a set of numbers entered as a vector
n = length(h);
s = sum(h);
meanval = n/s
end

To use this function, either in the command window, or in another script


file, we would call the function as shown:

Note that the name of the file (meancalc.m) must be the same as the name
of the function (meancalc). C is a vector of numbers given as input
while calling the function and the output of the function (the average
value) is saved in a variable called avg. avg can now be used for
further calculations if needed. For example, to calculate the standard
deviation. Do you remember the formula?
6. Write a script which calls the function that you wrote in
question 5, for r = 0.5, and increasing values of n, starting
from n = 10. Also evaluate the series sum as 1/(1-r). Calculate
error and implement a stopping criterion i.e. Calculate
percentage difference between the value that you have calculated
and 1/(1-r). If this difference if below some threshold, then you
can accept the series sum.
7. Write a script to find the zero of f(x) = 4x3-x2+3x-2 in the
range (-1,1) using bisection algorithm. Check your solution by
plotting the function.
8. Write a script to create a surface plot of = cos cos
the range -5 to +5 for both x and y

(2 +2 )
4

in

Вам также может понравиться