Hello everyone, this is my first post and I’m actually a quite excited to write for the first time in the World Wide Web 🙂 Today I’m going to show you what is Matlab and how to use it. When you launch Matlab this is what you’ll see:
This is the Matlab user interface. Where you find the folder you’re working on, the command window where you enter your commands and the variables workspace where you explore variables that you create. The main commands you need to know to start are related to matrices, expressions, plotting and flow control. Now we see how to start to use them.
BASIC OPERATIONS
a=2
- If you don’t write a semicolon (;) at the end of the command, Matlab will show you the variable that you’ve just created.
a=
2
- You should always comment your code so that anyone can use it, understand it and modify it as necessary. To put a comment in your code use “%”.
%Hi, this is a comment!
- Now we see how simple it is operate with basilar math operations; subsequently, we’ll create an array with rows and columns and we’ll do some operations with it 😉
a=2; b=5; %Sum c=a+b; %Subtraction d=a-b; %Multiplication e=a*b; %Division f=a/b;
% 3 elements in a single row a=[ 1 2 3] %Creation of an array with row and columns b=[5 8 1; 4 9 3; 4 3 1] b = 5 8 1 4 9 3 4 3 1 %Creation of an array with random numbers in a single column with 5 rows r=rand(5,1) r = 0.0975 0.2785 0.5469 0.9575 0.9649 %Creation of an array with zeros in 2 columns with 8 rows z=zeros(8,2) z = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
%Sum a constant to an array. a= [ 1 5 9; 1 9 3; 4 52 7] a+5 a = 1 5 9 1 9 3 4 52 7 ans = 6 10 14 6 14 8 9 57 12 %Sin of an array. sin(a) ans = 0.8415 -0.9589 0.4121 0.8415 0.4121 0.1411 -0.7568 0.9866 0.6570
- As you see, if you write a+5 without specifying a variable, Matlab will attribute the result value to the “ans” variable.
a' ans = 1 1 4 5 9 52 9 3 7 %where array "a" was a = 1 5 9 1 9 3 4 52 7
- How to raise each element of a to the second power? Simple….Look!
a= [1 8 6; 1 8 3; 4 9 2] a = 1 8 6 1 8 3 4 9 2 b=a.^2 b = 1 64 36 1 64 9 16 81 4
- Obviously, with Matlab we can work with complex numbers 😉 ! To represent an imaginary part you can indifferently use i or j:
complexarray=[2+3i; 4+5j] complexarray = 2.0000 + 3.0000i 4.0000 + 5.0000i
2D-PLOTS
- We want to create a 2D plot of cos function from 0 to 2Ï€. Ok, first of all create a variable x that is from 0 to 2Ï€. We “cut” this interval using:
x = 0:pi/100:2*pi;
- With this command, we’ve just created an array of 201 values with an increase of 3.14/100 each: 0,0314159265358979 0,0628318530717959 etc until you get 6.28 (2Ï€).
y=cos(x);
- This is also an array of 201 values!!!
- Ok now we can get the plot with:
plot(x,y)
- Perfect, now I’m going to show you how to label the axes, add a title and modify the line specifications 🙂
xlabel('x label') ylabel('y label'); title('Cos function')
plot(x,y,'g:')
- Ok, and if I want to show 2 or more plots in a window? Simply write the command “hold” typing
hold on
- In this way, we’ll get:
- In the present case I used these commands:
hold on z=sin(x); plot(x,z)
3D PLOTS
- Do you want to create a 3D plot? Ok follow these simple steps:
- We have to create a meshgride
- Define the function in two variables
- Create the surface plot
- Example 1: Create a hyperbolic paraboloid. The equation is:
[X,Y]=meshgrid(-10:0.1:10); Z=X.^2-Y.^2; surf(X,Y,Z)
- Example 2: Create a sphere. The equation is:
where the x, y and z are:
%sphere with a radius of 9 r=9; %we're defining an array from -pi/2 to pi/2 phi=linspace(-pi/2,pi/2,200); %we're defining an array from 0 to 2*pi theta=linspace(0,2*pi,100); %creation of meshgrid [phi,theta]=meshgrid(phi,theta); %array of x x=r*cos(phi).*cos(theta); %array of y y=r*cos(phi).*sin(theta); %array of z z=r*sin(phi); %create the surface! surf(x,y,z)
- If you want to create a sphere with radius of 1 you can write these simple commands:
figure sphere
FLOW CONTROL
- “For” loop syntax:
for index = values
statements
end - Example: I want to create a variable called poweroftwo that represents an array from 2^1 to 2^10 and then create a plot for it.
for i=1:10 poweroftwo(i)=2^i; end x=linspace(1,10,i); plot(x,poweroftwo,'rx')
- “If” loop syntax:
if expression
statements
elseif expression
statements
else
statements
end - Example: I’d like to know if in an array there’s some odd or even number.
%Create an array (1000X1) with random numbers from 1 to 10 randomarray=randi(10,1000,1) %The operator ~= means "not equal to" if any(mod(randomarray,2) ~= 0) disp('There is at least one odd number.') end if any(mod(randomarray,2) == 0) disp('There is at least one even number') end
- “While” loop syntax:
while expression
statements
end - Example: I want to calculate a factorial’s number.
n = 5; f = n; while n>1 n = n-1; f = f*n; end %num2string convert a number to string! disp(['n! = ' num2str(f)])
I hope you’ve enjoyed this short tutorial about matlab. This is maybe only 1% about matlab. You should experiment and try on your own 🙂 Have a great work!!!
Leave a comment if you have any suggestions 🙂