Академический Документы
Профессиональный Документы
Культура Документы
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.
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)
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
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