0% found this document useful (0 votes)
5 views

Useful functions

Uploaded by

minhtrihuynh1812
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Useful functions

Uploaded by

minhtrihuynh1812
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Applied Physics Department, FAS, HCMUT Matlab Projects – Physics 1

Operator/Function Purpose Example


Semicolon (;) end of statement clear all; % Clear memory
% a comment
clear deletes all (or the specified) clear x;
variable(s) from the
memory.
clf Clear graphics figure clf;
window
clc Clears command window
pi π
Inf Infinity ∞
syms Declares varriables syms x %symbolic variable x
Colon (:) generates regularly spaced for iCharge = 1:NCharges;
elements, creates vectors and x = [-10 : 0.01 : 10];
represents an entire row or
column
asterisk (*) Scalar and matrix 4*pi*Epsilonα
multiplication operator.
^ Scalar and matrix R = sqrt(x^2 + y^2);
exponentiation operator. C^2/(N m^2)
Parentheses ( ) encloses function arguments Fx = Fx+ q(iCharge)*q(jCharge)*xij/Rij^3;
and array indices x= dsolve('D2y=(-dmdt*eu)/(m0+dmdt*t)-
g','Dy(0)=0','y(0)=0');

Brackets [ ] encloses array elements.


== Equal to if( x == 1 )
~= Not equal to if( x ~= 1 )
Sqr(x) x2
Sqrt(x) Assign x = sqrt(16)
fplot Intelligent plotting of xplot(i)=xfunc(aalpha, hh, mm, tplot(i), vv0);
functions. yplot(i)=yfunc(aalpha, gg,hh,mm, tplot(i),vv0);
grid Displays gridlines.
plot Generates xy plot plot(xplot,yplot)
xlabel Adds text label to x-axis. xlabel('Range (m)');
title Puts text at top of plot.
disp Display words on the screen Disp (‘Amazing good job ’);
figure Opens a new figure window.
close Closes the current plot if( x == 0 )
close(gcf); % Close the figure window
elseif( x == 2 )
while While loop while(x ~= 0)
Repeats a statement or group y = x/2;
of statements while a given
condition is true.
bar Creates bar chart. bar(x,y), xlabel('Student'),ylabel('Score');

1/1
Applied Physics Department, FAS, HCMUT Matlab Projects – Physics 1

dot Dot product of two vectors dot(a, b);

cross Cross product of two cross(a, b);


vectors
meshgrid generates a matrix of [x,y] = meshgrid(-2:.2:2);
elements that give the range g = x .* exp(-x.^2 - y.^2);
over x and y along with the surf(x, y, g)
specification of increment in print -deps graph.eps
each case
surf create a surface plot
solve Solves algebraic equations y = solve('x-5 = 0')
the result: y = 5
dsolve Solves differential equations Let us take up a simple example of a first order differential
symbolically equation: y' = 5y.
s = dsolve('Dy = 5*y')
the result: s = C2*exp(5*t)
roots Solves algebraic equations The following example solves the quadratic equation x2 -
7x +12 = 0
y = roots([1, -7, 12]);
The following example solves the fourth order equation
x4 − 7x3 + 3x2 − 5x + 9 = 0.
v = [1, -7, 3, -5, 9];
s = roots(v);
diff computes symbolic syms t
derivatives f = 3*t^2 + 2*t^(-2);
diff(f)
ans =
6*t - 4/t^3
diff(f,n) computes higher derivatives f = x*exp(-3*x);
of a function f diff(f, 2)
ans =
9*x*exp(-3*x) - 6*exp(-3*x)
int calculates integral syms x
int(2*x)
the result: ans = x^2

int Definite integral


to calculate the value of
int(x, 4, 9)
the result: ans = 65/2

1/2

You might also like