Introduction to Matlab |
The following Matlab examples illustrate some commonly used Matlab syntax. Consult this list if you need to know small details (where punctuation occurs for example).
x = ( 22/7 -16 + 1.9 )*3^2 ;
y = exp( -pi*i ) ;
ezplot('x^2/(1+x^2)')
ezplot('x^2+y^2-4',[0,2],[0,2])
ezplot( 'cos(t)','sin(t)',[0,2*pi])
fplot( '[ sin(x), x-x^3 ]',[0,pi] )
ezsurf( 'xy^2 / ( x^2+y^4 )' )
ezsurf('sqrt(1-t^2)*cos(s*pi)', ...
'sqrt(1-t^2)*sin(s*pi)', 't', [-1,1] )
x = 0:.05:1 ;
fx = ( 1 + x ) .* cos( x ) ./ ( 3 - x.^2 ) ;
plot( x,fx,'--' ) ;
x = -2:.2:2 ; y = 0:.2:2 ;
[X , Y] = meshgrid( x,y ) ;
Z = X .* Y .* exp( -( X.^2 + Y.^2 ) ) ;
surf(X,Y,Z)
A = [ 2 3 4; 5 6 7; -1 -1 1] ;
col1 = A(:,1) ; row2 = A(2,:) ;
A_lower = A(2:3,2:3) ;
odd = 1:2:9 ; odd = odd' ;
b = [ 1;2;3 ] ; x = A \ b ; % Solve A x = b
[ U,Lam ] = eig(A) ; % Find eigenvalues
% & eigenvectors.
I = eye( 3 ) ; % Set up special matrices
S = rand(4,1) ;
T = diag( ones(4,1),-1 ) ...
+ diag( -2*ones(5,1) ) ...
+ diag( ones(4,1),1 );
x = 3 ;
for k = 1:30
x_new = ( x + 3/x )/2 ;
if abs( x - x_new ) < 1.e-12 , break , end
x = x_new ;
end
function yp = ff( t,y ) % A new function.
yp = y / ( 1+ t^2 ) ;