A Zeros (3,5) B Ones (3,4) C Eye (3) % Diagonal Matrix
A Zeros (3,5) B Ones (3,4) C Eye (3) % Diagonal Matrix
A Zeros (3,5) B Ones (3,4) C Eye (3) % Diagonal Matrix
Important Matrices
>> A=zeros(3,5)
>> B=ones(3,4)
>> C=eye(3) % diagonal matrix
Matrix-Array Operations
>> n=[5:-1:1]
>> D=A-n
>> D=D.*3
>> D./3
>> D.\3
>> a = [0 1+7];
>> b = [a(2) 7 a];
>> b(1,4)=28;
>> b(3)=99;
>> b
>> c = [1 2; 3 4];
>> d = zeros(size(c));
User Input
Output Formats
1
05.03.2020 E.D.
>> clc
>> clear all
>> close all
Built in Functions
High number of built-in functions are ready for use. Check octave documentation.
Plotting in Octave
x = 0:pi/100:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x);
plot(x,y1,'k-',x,y2,'b--');
title ('Plot of f(x) = sin(2x) and its derivative');
xlabel ('x');
ylabel ('y');
legend ('f(x)','d/dx f(x)','tl')
grid on;
help linspace
2
05.03.2020 E.D.
Plotting in 2D Arrays
clc,clear,close all
x = 0:0.1:10;
y = zeros(length(x),4);
y(:,1) = sin(x);
y(:,2) = cos(x);
y(:,3) = sin(x).^2;
y(:,4) = cos(x).^2;
plot(x,y)
clc,clear,close all
x=linspace(0,2*pi,10);
y=x.^2;
figure 1
bar(x,y)
figure 2
barh(x,y)
figure 3
compass(x,y)
figure 4
explode=ones(1,length(x))
pie(x)
figure 5
pie(x,explode)
figure 6
stem(x,y)
figure 7
stairs(x,y)
figure 8
semilogx(x,y)
figure 9
semilogy(x,y)
figure 10
loglog(x,y)
figure 11
polar(x,y)
3
05.03.2020 E.D.
Application
Write a Matlab script that calculates average, mininimum and maximum grades
of students and draw a bar graph of the grades. The number of the students
and the grades of the students need to be taken from the user. (input
function)