July 7th, 2008
To perform symbolic computations, use syms to declare the variables you plan to use to be symbolic variables. Consider the following series of commands:
syms x y
(x+y)*(x+y)*(x+y)
ans =
(x+y)^3
now you can expand this using the command expand(ans) and the output will be:
x^3+3*x^2*y+3*x*y^2+y^3
this can again be shortened or in technical terms factorized by using the command factor(ans)
MATLAB has a command called simplify, which can sometimes be used to express a formula as simply as possible. For example,
simplify((x^3 - y^3)/(x - y))
ans=
x^2+x*y+y^2
Did you know ??
cos(pi/2) will give the output as 6.1232e-17
whereas cos(sym(’pi/2′)) will give the exact value 0
This is because typing ‘pi’ in MATLAB gives an approximation to ∏ accurate to about 15 digits.
Tags: algebraic, expand, factor, matlab, operations, symbolic
Posted in Uncategorized | No Comments »
July 5th, 2008
Matrix factorizations help a lot to simplify the equation solving capabilities. System of linear equations involving factorized matrices can be solved easily and quickly with forward and/or backward substitutions.
(1)Cholesky factorization:
It expresses a symmetric matrix as a product of its upper triangular matrix and its transpose. Cholesky matrix is obtained by using the function: B=chol(A), where A is the original coefficient matrix and A=B’B. Not all symmetric matrices can be factored in this way; the matrices that have such a factorization are said to be positive definite. For a linear equation system:
Ax=b , eventually becomes
B’Bx=b
Because the backslash operator recognizes triangular systems, this can besolved in MATLAB quickly with x=B\(B’\b)
If A is n-by-n, the computational complexity of chol(A) is O(n3), but the complexity of the subsequent backslash solutions is only O(n2).
(2)LU factorization:
LU factorization, or Gaussian elimination, expresses any square matrix A as the product of a permutation of a lower triangular matrix and an upper triangular matrix where A=LU. It can be obtained again by using a simple command: [L,U]=lu(A).
The LU factorization of A allows the linear system Ax=b to be solved quickly and easily with:
x=U\(L\b)
Hope this helps… Good luck
Tags: cholesky, functions, LU factorization, matlab, matrix, operations
Posted in Uncategorized | 1 Comment »
July 4th, 2008
Guys here are some useful and uncommon matrix functions in matlab:
eye(m,n) returns a m-by-n rectangular identity matrix and similarly
eye(n) returns a square identity matrix of size n
The Kronecker product, of two matrices is the larger matrix formed from all possible products of the elements of A with those of B. If A is p-by-q and B is m-by-n, then the size of the resulting Kronecker matrix is pm-by-qn. This can be obtained by using the command kron(A,B)
A pascal matrix is a symmetric positive definite matrix with integer entries, made up from Pascal’s triangle.
This is obtained by using command : pascal(n) which returns a n-by-n pascal matrix. Similarly the command pascal(n,classname) gives a pascal matrix of class classname, for instance classname can be ’single’ or ‘double’.
The p norm of a vector x is defined as:
||x||p = (∑|xi|p)1/p
The p norm of the vector can be found by using the command norm(x,p)
The common values of p are 1,2 and ∞ hence commands are: norm(x,1), norm(x,2) and norm(x,inf)
The p norm of a matrix A is defined as:
| ||A||p = |
max x |
||Ax||p |
| ||x||p |
The p norm of the matrix can be found similarly by using the command norm(A,p)
The common values of p are 1,2 and ∞ hence commands are: norm(A,1), norm(A,2) and norm(A,inf)
Hope this proves to be useful, will come with more matrix stuff soon.
Tags: commands, identity, kronecker, matlab, matrix, norm, operations
Posted in Uncategorized | No Comments »
July 4th, 2008
Hello guys this is not one of the very common errors you receive in matlab but is very important. I encountered this error while running KD’s btp program.
The error looks something like this:
Error using ==> maple
Error, integer too large in context
Error in……..
Error in……..
This is generated while using symbolic toolbox, say for instance if you use
sqrt(-1) to generate the complex number symbol ‘i’
This may be a problem with the way memory management is performed by Maple. The only potential solution at this point of time is to wrap your call to a symbolic calculation that operates on numbers with large numbers of digits and clear the Maple function using one of the following commands:
maple clear
% or
maple restart
% or
clear maplemex
Alternatively you may increase the frequency of automatic garbage collections, using the following command:
maple(’kernelopts(gcfreq=10000)’);
Tags: error, integer, large, maple, matlab, solution, symbolic toolbox
Posted in Uncategorized | No Comments »
July 3rd, 2008
If your data file contains a mix of alphabetic and numeric ASCII data, the textread function can be used to import the data. It returns multiple output variables and the data type of each variable can be specified by the user.
For example the file xyz.dat contains the following data:
John GradeA 55 Yes
Tom GradeB 45 No
To read the entire contents of the file xyz.dat into the workspace, specify the name of the data file and the format string as arguments to textread. In the format string, you include conversion specifiers that define how you want each data item to be interpreted. For example, specify %s for strings data, %f for floating point data, and so on.
In this example, textread reads the file xyz.dat, applying the format string to each line in the file until the end of the file.
[names,grades,value,answer] = textread(’xyz.dat’,'%s %s %f %s’,1)
your output should be:
names =
‘John’
‘Tom’
grades =
‘GradeA’
‘GradeB’
value =
‘55′
‘45′
answer =
‘Yes’
‘No’
If your data uses a character other than a space as a delimiter, you must use the textread parameter ‘delimiter‘ to specify the delimiter. For example, if the file xyz.dat used a semicolon as a delimiter, use this command
[names,grades,value,answer] = textread(’xyz.dat’,'%s %s %f %s’,… ‘delimiter’,';’)
Tags: alphabetic, data, function, import, matlab, numeric, textread
Posted in Uncategorized | No Comments »
July 3rd, 2008
Similar to binary data, exporting ASCII data in Matlab is also pretty simple
Some of the useful functions are:
csvwrite– Numeric Data, delimiter:comma, used for exporting data to spreadsheets in .csv format
dlmwrite–Numeric Data, delimiter:any character, used for exporting data to ASCII delimited file
save–Numeric and alphabetic data, delimiter:any character, used for exporting to text files
fprintf–Numeric and alphabetic data, delimiter:any character, requires use of fopen and fclose commands which is discussed in other posts.
diary–Numeric Data and cell array, delimiter:only spaces, Easy to use, exporting data to matlab workspace
Tags: ASCII, data, exporting, functions, matlab
Posted in Uncategorized | No Comments »
July 2nd, 2008
ASCII data can be imported in matlab using several functions depending on the data type
csvread– Numeric Data, delimiter:comma, used for importing spreadsheets in .csv format
dlmread–Numeric Data, delimiter:any character, used for reading ASCII delimited file
textread–Numeric and alphabetic data, delimiter:any character, used for reading text files
fscanf–Numeric and alphabetic data, delimiter:any character, requires use of fopen and fclose commands which is discussed in other posts.
load–Numeric Data, delimiter:only spaces, Easy to use, importing data from matlab workspace
Tags: ASCII, data, function, import, matlab, read
Posted in Uncategorized | No Comments »
July 2nd, 2008
Binary data export commands are:
avifile— exporting audio visual data in avi format
xlswrite–export data in microsoft excel spreadsheet format
imwrite–export images in various formats such as .jpg, .bmp, .tiff, .png, .hfd, .gif, .pcx, .xwd
wavwrite–exports sound data in microsoft windows format (.wav)
save– matlab workspace files (.mat)
auwrite–exporting sound data in sun microsystems format (.au)
addframe–capturing snapshots of current axes and adding them to avi file
Tags: binary, data, export, functions, matlab
Posted in Uncategorized | No Comments »
July 2nd, 2008
If the data is rectangular, that is, each row has the same number of elements, the simplest command to use is the load command.
other binary data importing commands are:
aviread — importing audio visual data in avi format
xlsread–import data in microsoft excel spreadsheet format
imread–import images in various formats such as .jpg, .bmp, .tiff, .png, .hfd, .gif, .pcx, .xwd
wavread–imports sound data in microsoft windows format (.wav)
load– matlab workspace files (.mat)
auread–importing sound data in sun microsystems format (.au)
Tags: binary, data, functions, import, matlab
Posted in Uncategorized | No Comments »
July 2nd, 2008
Lets start with the funny stuff…… here are the few easter eggs for matlab
1. If you need some philosophical guidance from Matlab the do the following:
>>Enter ‘why’ at the matlab prompt (without quotes)….. and keep repeating
2. Special simulations
>>toilet
in the prompt and wait for a toilet simulation to pop up with flushing sounds and everything!
>>shower
for the shower simulation
Hope you like them… will keep coming with more of them in future but lets go to some serious work now…
Tags: easter egg, funny, matlab, simulation
Posted in Uncategorized | No Comments »