You are on page 1of 3

27.02.2020 E.D.

Numerical Methods in Engineering

Computer Lab Lecture 1 Introduction to Octave/MATLAB

Main Octave interfaces

 Workspace
 Current Folder
 Commend Window
 Editor
 Documentation

All Things Saved ss Matrix in Matlab/Octave

>> b=1;
>> c=[1:5;6:10]
>> size(c)

logical class (1 for true 0 for false) example below;

>> 1==1
>> 3==4
>> 1~=3

complex numbers

>> sqrt(-1)

character class

>> a='apple' % number of characters are important

list of packages installed in octave

>> pkg list

To install a new or updated package version manually, the package file can be downloaded from the Octave
Forge website to the working directory and can be installed using:

>> pkg install package_file_name.tar.gz


>> pkg load package_name

Symbolic Package

>> pkg load symbolic

1
27.02.2020 E.D.

>> A=(x-y)*(x+y)^2+x^2
>> B=subs(A, x, y*x^2) % changes x in the A equation with y*x^2
>> simplify(A)
>> sym (1/3+1/24)

Cleaning the command window and workspace

>> clc
>> clear all
>> close all

Some Built in Functions in Matlab

>> cos(pi/2) %check answers


>> cos(sym(pi/2)) %check answers
>> ezplot('x^4-5*x^3') % easier way to draw a graph

Colon/Row Operations in Matlab

>> B=[1,2,3,4] %row vector


>> C=[1;2;3;4] %column vector
>> B'==C
>> C'==B
>> E=[1:10;2:2:20;3:3:30]
>> I=E(:,1)
>> F=E(3,4)
>> P=E(:,1:4)
>> K=E(1:2,:)

Plot Example

>> X=[0:0.1:10]; % a row vector starting from 0 to 10 with 0.1 step size
>> Y=randi([-10 10],size(X,2),1);% random integers -10 to 10 same size X
>> Z=Y.^2;
>> plot(X,Y,'ro')
>> plot(X,Y,'ro',X,Z,'k*')
>> plot3(X,Y,Z)

Export/Import .csv file to Octave

>> csvwrite('example.csv',E)
>> B=csvread('example.csv')

for Loop Example (in editor)

s=0; % initial value outside of the for loop


for i=1:10
s=s+i;
if s>40;

2
27.02.2020 E.D.

disp('s > 40')


disp('i= ')
disp(i)
break
endif
endfor

Anonymous Function Examples

>> a=5;
>> f = @(x)x^2 % anonymous function name of f which calculates f(x)=x^2
>> f2=@(x,y)x.^2+y.^2 % function with two input, f(x,y)=(x^2+y^2)
>> f2(3,5)

Application 1

Download ‘tutorial_1.csv’ in CMS. Read tutorial_1.csv file in Octave and


plot temperature vs density and temperature vs cp in same subplot. And plot
temperature vs k.viscosiry and temperature vs t. conductivity in same subplot
Use subplot(), plot() and csvread() functions use help command for more
information.

Application 2

Generate a 25x25 matrix I including random integers range from 0 to 100 (use
randi() function). calculate how many elements of the matrix are greater
than 80 (use for loop).

You might also like