Matlab Tutorial 7: Common Programming Structures and Conditional Statements

In this tutorial we will deal with some common programming structures and also alternative conditionally statements. These statements exist as well in almost all high-level programming languages. In Matlab a common programming construction is a if or a switch statement. To repeat a number of statements, can be solved with a for or a while loop.
In the following examples we will see how this is implemented in Matlab, but nevertheless how good programmers we are, some mistakes will happen. This also motivates a brief view of the debugger in Matlab.

For Loop in MATLAB

The general expression for a for-loop is:

for variable=expression
control statements
end

Example 1:

Let us write an m-file that calculates the sum: The sum to be calculated is: 1+1/22+1/32+…..+1/102.

i=1;
the_sum=0;
for i=1:10
the_sum=the_sum+1/(i^2);
end
fprintf('The sum of 10 elements gives: %f ', the_sum)

The execution of the code in Matlab command window gives: The sum of elements gives: 1.549768
Read more »

Matlab Tutorial 6: Analysis of Functions, Interpolation, Curve Fitting, Integrals and Differential Equations

In this tutorial we will deal with analysis of functions, interpolation, curve fitting, integrals and differential equations. Firstly, we will need to use polynomials and therefore we have to be familiar with the representation of these. A general polynomial looks like: p(x)=anxn + an-1xn-1 +……….+ a1x + a0 and is represented by a vector in Matlab:
p=[ an an-1 ……. a1 a0 ]

Here we have a list of basic commands dealing with polynomials.

polyval(p,x): Calculates the value of polynomial p for different x. If x is a vector then the polynomial is evaluated for each element in the vector x.
poly(A): Gives a vector that represents the characteristic polynomial for the matrix A.
roots(p): Gives a vector with the zeros for the polynomial p(x)=0.
polyder(p): Gives a vector that represents the time-derivative of the polynomial p(x). The coefficients are sored in the vector p.
conv(p,q): Multiplies the polynomials p and q with each other. Returns a coefficient vector.
polyint(p): Integrates the polynomial p analytically and uses the constant of the integration c. The constant c is assigned to 0, if it is not explicitly given.
residue(p,q): Makes a partial fraction expansion of p(x)/q(x).

Read more »

Matlab Tutorial 5: Linear Equations

In this Matlab tutorial we will deal with linear equations, the least square method, condition numbers over & under-determined equations.

Let’s start with an example.

Example 1

We have an equation system with three unknown variables and three equations. What will be the solution to the system below?

3w-2y+4z=8
5w+8y-6z=-5
9w-2y+7z=-17

Assume a matrix A containing the coefficients multiplied with x, y and z, and a vector with the numbers on the right-hand side of the equations. We can thus rewrite our equations as:

AX=b , where X contains the unknown (w, y and z), A and b are shown below.

b=     A=
  8    3 -2  4
 -5    5  8 -6
-17    9 -2  7

How will we find the solution?

>> X=inv(A)*b

or

>> X=A\b

both gives a correct answer. The last method produces a Gaussian elimination if A is a quadratic matrix. In our case X becomes:

X=
-36.7778
71.2778
65.2222

This is no exact solution, only an approximation. Try also a specific command: Read more »

Matlab Tutorial 4: Data Analysis and Statistics with Matlab

This tutorial covers data analysis and statistics using Matlab.

Histogram Charts in Matlab

The elements of a vector can be displayed with bars or histograms. To create a histogram you need to divide the elements in to classes and count how many elements that belongs to each class. Then present them as rectangular bars in a diagram. The height of the rectangle is equal to the number of elements in that class. Read the vector.

Matlab Histogram Example

Matlab Histogram Example

  • hist(x) Plots a histogram with 10 intervals (default) for the elements in vector x.
  • hist(x,n) Plots a histogram with n intervals for the elements in vector x.
  • hist(x,y) Plots a histogram with arbitrary intervals. These are given in vector x.

Introduce a histogram with 15 intervals for the vector x above. It is rather difficult to see how long the intervals are. Maybe its better to introduce a histogram with 6 intervals since we know the difference between the maximum and minimum value.

>> hist(x,6)

If we don’t know the dataset, we can define the intervals that we are interested to have in the histogram. Suppose we want the integer values between 0 and 10.

>> y=0:10;
>> bar(x),grid
>> title('bar for vector x')

Histogram Bars with Grid in the Background

Histogram Bars with Grid in the Background


Read more »

Matlab Tutorial 3: Strings in Matlab

The concept of variables is fundamental in all programming. In Matlab you introduce variables and assign values to them all by yourself. When you assign a value to a variable its previously assigned value will be overwritten. You can at any time find out the actual value of a variable simply by writing the variable name in the command window (without ;).

The class is decided by the assigned value.

>> a=1; % the class is double and the storage size is 8 bytes.

Open and check workspace.

In this laboratory we will investigate strings and conversions of strings among other things. A string is stored as a row vector, so that every element represents a character. Each character is accessible by the name of the vector and its indice.

Exercise 1: Assigning a variable to a string of characters

>> words='This is a string'

Press on enter to see the content of the variable words. Words has the size 1×16 elements. The class is char (character) and storage size 32 bytes.

>> words(2) % gives the second element in the variable words.
ans = h
>> words(2)='t' % replaces the second element in the vector with t.

Sooner or later we need some form of interactivity. For instance to get input from the user. To prompt the user for an input:
Read more »