Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

A Zeros (3,5) B Ones (3,4) C Eye (3) % Diagonal Matrix

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

05.03.2020 E.D.

Numerical Methods in Engineering

Computer Lab Lecture 2 Introduction to Octave/MATLAB

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

>> in1 = input('Enter the value: '); %in1 stores as double


>> in2 = input('Enter data: ','s'); %in2 stores as character

Output Formats

1
05.03.2020 E.D.

>> s=num2str ([1, 1.34; 3, 3.56], "%5.1f")


>> fprintf('The value of pi is %f \n',pi)
>> fprintf('The value of pi is %5.2f \n',pi)

Cleaning the command window and workspace

>> clc
>> clear all
>> close all

Built in Functions

 High number of built-in functions are ready for use. Check octave documentation.

>> help max


>> help randi
>> x=randi([0 100], 1, 10);
>> max_x=max(x)
>> [max_x index]=max(x)% 2 outputs (maximum value and its index)

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)

Different Plot Functions

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.

Relational and Logical Opeators

Anonymous Function Examples

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)

You might also like