Sunteți pe pagina 1din 6

* + - / \ ^

%
format long
format short
format rat
vpa(x, 100)
;
,
> < >= <= == ~=
if x >= 10, y = 5;
end
if x == 20
y = 5;
elseif x == 30 y = x;
else
y = 0;
end
switch m
case 1
x = 10
case 2
x = 50
otherwise
x = 60
end
&
|
for x = 1 : 2 : 10
%do stuff
end
while m < 10
m = m + 1;
end
break
continue
clear
clear x y z
clc
x = input('message');
x = input('msg', 's');
\n
\\
disp(['x' ' = ' '10']);
disp(['x', ' = ', '10']);
disp(10);
fprintf('10\n11\n12\n');
fprintf('%2.3f\n', 12.6);
%out 12.600

fprintf('%7.2f\n', 12.657);
%out

12.66

fileID = fopen('1.txt', 'w');


fprintf(fileID, 'anything');
fclose(fileID);

Arithmetic Operators. Note: a/b = b\a


Comments Complete on the next line
15 digits, (long g) for no zeros if not needed
5 digits
ratio of smallest integers
Returns x as 100 digits precision.
Avoid print result
Many Orders on the same line (can use ;)
Conditional Operators
if statement (condition)
Using if with elseif with else ... (without , is ok)
NOTE: elseif else need one end,
but else if needs an end for each if
inside
switch statement (conditional)

Logical statement and


Logical statement or
For loop
Begins with 1 & ends with 10
Steps by 2: 1 3 5 7 9.
While loop

Terminates for - while loops where its inside.


Go to the next iteration of for - while loops.
Clears all variables.
Clears the specified variables.
Clears the command window.
Outputs message - waits for any value entered for x.
Indicates that the input value is a string.
New Line in a string
Just output \
Outputs vector or array or variable

(2 & 7) -> Minimum no of characters to print.


(3 & 2) -> No of digits after dot (Precision).
f -> Convert to float type (Fixed point).
NOTE: can use more than one variable to print.
for more help type doc fprintf
Using files (in workspace directory)
for help about w type doc fopen

*
.*

/
./

\
.\

^
.^

x = [1, 4, 7, 0, 3, 5];
x = [1; 4; 7; 0; 3; 5]';
x(4)
%Outputs 0
x = [0.2; 0.4; 0.9];
x = [0.2, 0.4, 0.9]';
x(2)
%Outputs 0.4
x = 4 : -2 : -4;
x = (4 : -2 : -4)';
x(4) = 3;
x = [2, 3];
x = [0, x, 4, 5];
x = [0; 1; 2; 3; 4; 5];
x = x(2 : 5);
x = 'hello';
x = ['h', 'e', 'l', 'l', 'o'];
y = [1, 2, 3; 4, 5, 6];
y(2, 3)
%Outputs 6

Arithmetic Operators.
Dot is used to calculate for each element not for
the whole array or matrix.
Declaration of a row array (' for transpose)
(Both are equivalent)
Declaration of a column array
(Both are equivalent)
As
As
As
As

x
x
x
x

=
=
=
=

[4,
[4;
[0,
[0,

2,
2;
0,
1,

0,
0;
0,
2,

-2, -4];
-2; -4];
3];
3, 4, 50];

As x = [1; 2; 3; 4];

length(x)
size(x)

Same thing.
NOTE: a string is an array of characters
Declaration of a 2-dimensional array: 1 2
4 5
Get second column of y.
No of elements of x
Returns no of rows & no of columns

sin(x)
cos
tan
csc **
sec
cot
ADD AN h
ADD AN a
ADD AN d

Sine
Cosine
Tangent
Cosecant
Secant
Cotangent
Hyperbolic. Ex: sinh, cosh
Inverse.
EX: acsc, asech
Use Degree instead of Radians. Ex: sind, acosd

log
log10
exp
i, j

Natural logarithm
Base 10 logarithm
Exponential. Ex: exp(1) %Outputs 2.718281828

x = y(:, 2)

real, imag
conj
angle
pi
inf

3
6

1
Real & Imaginary part of a complex number
Conjugate of a complex number
Phase angle. Ex: angle(1 + i) %Outputs 0.7854 (45o)

(For help about multidimensional arrays & matrices, type doc + function name)
max, min
sum
mean
sqrt
abs
sign
rem(x, y)
mod(x, y)
round
fix
ceil
floor

Maximum & Minimum of an array


Sum of the elements of an array
Mean value of the elements of an array
Square root
Absolute value (and magnitude for complex numbers)
+1 if x>0 and -1 if x<0 and 0 if x=0
Remainder of x/y division
if Y ~= 0, returns X - n*Y where n = fix(X/Y)
if Y ~= 0, returns X - n*Y where n = floor(X/Y)
Approximates towards nearest integer
Approximates towards zero
Approximates towards +
Approximates towards -

eye(m, n),
eye(n)
zeros(m, n), zeros(n)
ones(m, n), ones(n)
inv(x)
det(x)

Returns a m n, n n identity matrix


Returns a m n, n n matrix of zeros (null matrix)
Returns a m n, n n matrix of ones (unity matrix)
Inverse of a matrix (different from transpose ['])
Determinant of square matrix. (Scalar value)

p = [1 0 -26 0 25];
%As P = x4 26x2 + 25
roots(p)
conv(p1, p2)
[q r] = deconv(p1, p2)

Representation of a polynomial equation (like


vectors).
Returns a vector of the roots
Multiply the 2 polynomials and returns a new vector
q -> vector of division p1 by p2 using long division
r -> vector of remaining of division
Substitutes in the equation with k and returns value
Returns a vector of the 1st derivative of p
Returns a vector of the 1st integration of p
k -> constant of integration
x & y arrays coordinates.
Returns a polynomial that fits the points, and with
the specific degree.

polyval(p, k)
polyder(p)
polyint(p, k)
polyfit(x, y, degree)

plot(x,
plot(x,
plot(x,
plot(x,
plot(x,

y)
y,
y,
y,
y,

'+')
'--')
'LineWidth', 4)

':r', x, y, '+g')

title('Parabola no1')
xlabel('This is x axis')
zlabel('This is z axis')
text(5, 10, 'hey')
text(5, 0.5, 'hey',
'FontName', 'Arial',
'FontSize', [14], 'Color',
'r')
%This part in file1.m
function z = f(x)
z = cos(x);
%This part in file2.m
fplot('file1', [0, 10])
loglog(x, y)
semilogx(x, y)
clf
cla
axis([xMin xMax yMin yMax])
axis square
axis off
grid on
%Or off
hold on
%Or off
subplot(3, 4, 5)

[x, y] = meshgrid(x, y);


mesh(x, y, z)
surf(x, y, z)
h = contour(x, y, z, level);
clabel(h)
clabel(h, 'manual')

quiver(x, y, u, v, s)

Where x & y are arrays. The output is a line.


Mark + at all points, no line.
Dashed line. ':'for dotted, '-.' for dash dot.
Adjust line thickness.
Combined line & markers. r for red & g for green.
Can draw multiple curves this way
Write a title for the drawing.
Write something on an axis.
(All should be after plotting not before)
Write text @ specific coordinate.
(These changes can be applied to title and label)
For more info (doc text).
Plotting a function.

As plot but with log-log scale.


The log-log scale on xAxis only. (Can use semilogy)
Clear everything inside the graphic window.
Clear the curves only and redraw the axes.
Set axes limits manually.
Reshapes the drawing so that xAxis length = yAxis.
Hide axes.
Show or hide the grid.
On: The next graph command adds to the existing
drawing.
Plot multiple graphs, 3 rows, 4 columns, 5 is the
index of the current graph where we draw.
Repeat that to draw every graph with its index.
Convert the 1 dim arrays x & y to 2 dim matrices to
make like a grid for 3D plotting.
Plot a mesh.
Plot a shaded surface.
Plot a 2D contour, where level is an array of z
value for each contour. (Can remove h = )
Put values of z on each contour line.
Let user put values for desired contours manually.
(Can be used to plot an implicit function, by
setting level to [0, 0].)
Plot vectors (arrows).
x & y are arrays of coordinates, u & v are arrays
of direction coordinates, s is the vector scale.

syms a w s y
q = a*w - s/(y+3);
findsym(q, 1)
%y
findsym(q, 2)
%y,w
ans(2)
%,
symsum(x, n, m)
symsum(sym('y')^x, x, 5, m)
y = 'x^2-1';
ezplot(y, [-5, 5, -2, 10])
ezplot x^2-1, [-10, 10]
syms x
y = x^2-1;
factor(y)
%(x-1)*(x+1)
factor(x^2-3) %x^2-3
pretty(y)
pretty(x^2-1)
taylor(f, 4)
y = (x-1)*(x+1);
collect(y)
%x^2-1
expand(a*(x+y)) %a*x+a*y
simple(y);
simplify(log(x*y))
%log(x*y)
%(Error if negative)
syms x y positive
simplify(log(x*y))
%log(x)+log(y)
subs(a*x^2+1, a, b) %b*x^2+1
subs(x^2+a*x, a, -1) %x^2-x
isstr(f)
eval('7*5+1')

%36

solve('a*x^2 + b*x + c')


solve('a*x^2 - a = 5', a)
[x1, x2] = solve('x1^2 + 1
= 10', 'x2 + x1 = 10')
s = solve('x1 = 5*a',
'x2 + d*a = 10', x1, d);
s.x1, s.d
limit((1+x/n)^n, n, inf)
limit((1+x/n)^n, inf)
limit((1+x/n)^n)
limit(1/x)
limit(1/x, x, 0, 'left')
limit(1/x, x, 0, 'right')
diff('a*x^3+x^b')
diff('a*x^3+x^b', a)
diff('a*x^3+x^2', 2)
diff('a*x^3+x^2', a, 2)

Declare symbols. (Can be declared as a = sym('a'))


findsym chooses the default independent variable
by the unique, lower case, other than i & j, not a
part of a word, the closest to x. If none it chooses
x. If tie, the later one in the alphabet is chosen.
Returns the symbolic sum of series n, n+1, ,m-1, m
Second parameter, x, is being looped from 5 to m.
Plot a symbolic function.
Factorizing.
Another way:
factor(sym('x')^2-1)
Only works if it can be factored over the rational
numbers.
Display it natural. (Easier to read)
Expand f to 4 terms using Taylor series.
Collect coefficients with same power.
A second parameter can specify the variable.
Distribute products over sums.
Display every trial simplification function and its
result, and returns the shortest result.
Simplify representations of uncertain objects.
If we call expand, it will give second answer
without checking for negative values

Replace characters or strings

Returns 0 for numeric and 1 for string.


Evaluate a symbolic constant into a number.
As numeric; which isnt available in all versions.
Solve an equation for x (look findsym).
Solve it for the symbol a.
Solve many equations in many variables.
Specify variables, s is a structure.
Gets the limit of the function where n tends to .
This time x tends to inf (look findsym).
Here x tends to 0.
Answer is NaN (Not a Number).
Limit from the left = -.
Limit from the right = .
Differentiation.
(returns 3*a*x^2+x^b*b/x)
Differentiate for a.
(returns x^3)
Differentiate twice.
(returns 6*a*x+2)
Differentiate twice for a. (returns 0)

diff('[a*x, x^2; x, x^3]')


int('sin(s+2*x)')
int('sin(s+2*x)', 's')
int('sin(s+2*x)', pi/2, pi)
int('sin(s+2*x)', s,'m','n')
int('[a*x, x^2; x, x^3]')
dsolve('Dy = 1 + y^2')
%returns tan(t+C1)
dsolve('Dy=1+y^2','y(0)=1')
%tan(t+1/4*pi)
dsolve('Dy=1+y^2','y(0)=1',
'x')%tan(t+1/4*pi)
dsolve('D2y = cos(2*t)-y',
'Dy(0)=0', 'y(0)=1')

Returns same array differentiated.


Integration about x (findsym).
Integration about s.
Integration from pi/2 to pi.
Integration about s from m to n.
('' is necessary if u didnt declare syms s)
Returns same array integrated.
Solve differential equations. Dy means dy/dx.
t is the default independent variable.
Find constants with an initial condition given.
Specify x as independent variable.
D2 for double differentiation.

S-ar putea să vă placă și