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 |
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) |
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 »
Tags: Debugging in matlab, debugging tutorial for matlab, debugging tutorial in matlab, For loop, For loop example for matlab, For loop in matlab, how debug works in matlab, how to debug in matlab, If-else example, If-else example for matlab, If-else example in matlab, If-else in matlab, If-else sample in matlab, Interrupt commands, Interrupt commands example in matlab, Interrupt commands for matlab, Interrupt commands in matlab, interrupt example in matlab, logical operators, logical operators example for matlab, logical operators for matlab, logical operators in matlab, logical operators samle code for matlab, matlab codes, matlab courses, matlab examples, matlab try catch, matlab try catch example, matlab tutorial, Matlab Tutorials, Nested loop example for matlab, Nested loop sample code for matlab, Nested loops example code for matlab, Nested loops in matlab, Nested loops sample in matlab, Switch example for matlab, Switch example in matlab, Switch for matlab, Switch in matlab, Switch sample code for matlab, timing in matlab, try and catch example for matlab, Try-catch matlab, Try-catch-end example, Try-catch-end example for matlab, Try-catch-end matlab, While, while in matlab, while loop example for matlab, While loop in matlab
Matlab Tutorials | admin, July 5, 2010 8:47 am | Comments Off on Matlab Tutorial 7: Common Programming Structures and Conditional Statements
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 |
b= A=
8 3 -2 4
-5 5 8 -6
-17 9 -2 7
How will we find the solution?
or
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 |
X=
-36.7778
71.2778
65.2222
This is no exact solution, only an approximation. Try also a specific command: Read more »
Tags: coefficients, complex conjugated, cubical functions, diagonal elements, differential equations, eigenvalues, eigenvectors, equation system, linear equations, linear system, linearly independent, Matlab Tutorials, quadratical functions, sparse matrices, tic, toc
Matlab Tutorials | admin, June 13, 2009 4:06 am | Comments Off on Matlab Tutorial 5: Linear Equations