clc % clears screen 1+3 2^12 (3+2*i)*(2-4*i) % complex algebra abs(4-3*i) %absolute value sin(pi) cos(2*pi) log(2.718) %natural logarithm log2(1024) log10(1000) format long sqrt(5) pi format short sqrt(3) |
a=20.0 b=a-sin(3); a+b clear a a |
i %imaginary number j % same as i clock % current time (six elements) date % current date pi %3.14156 eps %smallest tolerance number in the system format long pi pi=2.14 pi clear pi pi |
v1=[1 2 3] % defines a 3-D row vector. v1=[1, 2, 3] % Same as above. Separator is either space or comma v2=[1;2;3] % this defines a column vector. v2=v1' %transpose of v1, i.e. column vector m1=[1 2 3;4 5 6;7 8 9] %defines a 3x3 matrix m1=[1,2,3;4,5,6;7,8,9]; %semicolon suppresses echo m2=[-4 5 6; 1 2 -87; 12 -43 12]; m2(:, 1) % Extracts the first column m2(2, :) % Extracts the second row m2(2,3)=10; % Assigns 10 to the (2,3) element of m2. m1*v2 %matrix multiplication inv(m1) %inverse of m1 [vec, lambda] = eig(m1) % eigenvectors and eigenvalues of m1 m1\m2 % inverse of m1 times m2, same as inv(m1)*m2 m1*v2 % matrix m1 times vector v2 a=eye(3) % 3x3 identity matrix a=zeros(3) % 3x3 matrix with 0 as components a=ones(3) % 3x3 matrix with 1 as components det(m1) % determinant of m1 |
|
a=[1 4 5; 8 1 2; 6 9 -8]; b=[4; 7; 0]; sol2 = inv(a)*b % or sol2 = a\b
x=[1: 2 :10]; % a sequence between 1 and 10 with an increment of 2. x=linspace(1, 9, 5); a sequence between 1 and 9 with equidistant 5 entries y=x; z=x * y % error z = x .* y; %works z = x / y ; %error z = x ./y ; % works |
x=[0:0.1:10]; %initial value, increment, final value
y=sin(x);
plot(x, sin(x)); %calls Gnuplot
x=linspace(0,2, 20) % between 0 and 2 with 20 divisions
plot(x, sin(x))
%%%%%%%%%%%%%%%%%%
x=[0: 0.2 : 10];
y=1/2 * sin(2*x) ./ x;
xlabel('X-axis');
ylabel('sin(2x)/x');
plot(x, y);
close;
%%%%%%%%%%%%%%%%%
t=[0: 0.02: 2*pi];
plot(cos(3*t), sin(2*t))
%%%%%%%%%%%%%%%%%%%%
x=[0: 0.01: 2*pi];
y1=sin(x);
y2=sin(2*x);
y3=sin(3*x);
plot(x, y1, x, y2,x, y3);
%%%%%%%%%%%%%%
xx=[-10:0.4:10];
yy=xx;
[x,y]=meshgrid(xx,yy);
z=(x .^2+y.^2).*sin(y)./y;
mesh(x,y,z)
surfc(x,y,z)
contour(x,y,z)
close
|


MATLAB generated graph
a=[1 2 3 4 5] % defines x4+2x3+3x2+4x+5 r=roots(a) % solves x4+2x3+3x2+4x+5=0. |
disp(a)
disp('Enter a number = ')
a=input('Enter a number =');
fprintf('The solution is %f\n', a);# %f and %d are available but not %lf
|
cd c:\temp cd 'c:\Documents and Settings\user\My Documents' pwd % present working directory cd % back to home directory myownfile % loading a script m-file named "myownfile.m" save myown.txt a, b % saves variables "a" and "b" to a file "myown.txt" load myown.txt |
function y=myfunction(x) y=x^3-x+1; |
function [x, y]=myfunction2(z) x=z^2; y=z^3; |
[a,b]=myfunction2(3); |
if a>2
disp('a is larger than 2')
end
for k=0:2:10
disp(2*k)
end
|
|
|
|
|
|
|
|
|
|
|