lecture notes of matlab

5
Matlab Lecture 1 % ones of 4 x4 y = ones(4) % ones % 1 row : 10 column i = ones(1,10) % zeros % 1 row : 10 column i = zeros(1,10) % eye % diagonal elements 1 i = eye(5) % inverted eye % diagonal elements 0 i = ~eye(5) % random numbers generation 5 x 5 i = rand(5) % Trignometric functions Plotting t = 0:0.0001:0.01 y = 3 * sin(2*pi*100*t); plot(t,y); title('Sin Wave'); xlabel('Time'); ylabel('Amplitude'); figure; t = 0:0.0001:0.01 z = 3 * cos(2*pi*100*t); plot(t,z);title('Cos Wave'); xlabel('Time'); ylabel('Amplitude'); % Use of Sub Plot command figure; subplot(2,1,1);plot(t,y);title('Sin Wave'); xlabel('Time'); ylabel('Amplitude'); subplot(2,1,2);plot(t,z);title('Cos Wave'); xlabel('Time'); ylabel('Amplitude'); % magic square of 4 x 4

Upload: salman-tariq

Post on 17-Dec-2015

230 views

Category:

Documents


1 download

DESCRIPTION

Some notes about coding in MATLAB.

TRANSCRIPT

% ones of 4 x4

Matlab Lecture 1

% ones of 4 x4y = ones(4)% ones % 1 row : 10 column i = ones(1,10)% zeros % 1 row : 10 column i = zeros(1,10)% eye % diagonal elements 1 i = eye(5)% inverted eye % diagonal elements 0 i = ~eye(5)% random numbers generation 5 x 5i = rand(5)% Trignometric functions Plotting t = 0:0.0001:0.01y = 3 * sin(2*pi*100*t);plot(t,y); title('Sin Wave'); xlabel('Time'); ylabel('Amplitude');figure;t = 0:0.0001:0.01z = 3 * cos(2*pi*100*t);plot(t,z);title('Cos Wave'); xlabel('Time'); ylabel('Amplitude');% Use of Sub Plot command figure;subplot(2,1,1);plot(t,y);title('Sin Wave'); xlabel('Time'); ylabel('Amplitude');subplot(2,1,2);plot(t,z);title('Cos Wave'); xlabel('Time'); ylabel('Amplitude');% magic square of 4 x 4 y = magic(4)% ones % 1 row : 10 column i = ones(1,10)% maximum % max value of i (first row)j = max(i)% manimum % min value of i (column wise)j = min(y)% LinSpacing command t = linspace(0,0.01,100);y = 3 * sin(2*pi*100*t);plot(t,y); title('Sin Wave'); xlabel('Time'); ylabel('Amplitude');figure;% lin spec (diff color and style)t = linspace(0,0.01,100);z = 3 * cos(2*pi*100*t);plot(t,z,'+g');title('Cos Wave'); xlabel('Time'); ylabel('Amplitude');% lin spec (diff color and style)t = linspace(0,0.01,100);z = 3 * cos(2*pi*100*t);plot(t,z,'-y');title('Cos Wave'); xlabel('Time'); ylabel('Amplitude');% Size Command y = size(t)% Length Command y = length(t)% Assigning complex value y = 7 + 6j% Real and complex parts g = real(y)h = imag(y)% Conjugate k = conj(y)% absulute and angle k = abs(y) % sqrt(r^2 + I^2)k = angle(y) % tan-1 (b/a)%equation solving linear and non linear%5x2 + 11x + 6 = 0. clear, syms x; % for construction symbolic objectseq = '5*x^2+11*x+6 = 0'; x=solve(eq,x) %*******************************clear, syms x y z a b c; % for construction symbolic objectseq = 'a*x^2+b*x+c = 0'; x=solve(eq,x) ;a=5;b=11;c=6; subs(x)%********************************% e2x=3yclear, syms x y; % for construction symbolic objectseq = 'exp(2*x) = 3*y';x= solve(eq,x) %***************************clear,syms x; % for construction symbolic objectseq = 'sin(x)-1/2=0'; x = solve(eq,x)decans=double(x)%******************************%solution lie b/w 1 to 10clear,syms x = maple('fsolve(sin(x)=1/2,x,1..10)')%simultaneous equation solving %2x-3y+4z = 5%y+4z+x = 10%-2z+3x+4y = 0clear, syms x y z; eq1 = '2*x-3*y+4*z = 5';eq2 = 'y+4*z+x = 10';eq3 = '-2*z+3*x+4*y = 0'; [x,y,z] = solve(eq1,eq2,eq3,x,y,z) double(x),double(y),double(z)%decimal%*****************************************% y = 2*exp(x)% y = 3-x^2clear, syms x y; eq1='y=2*exp(x)';eq2='y=3-x^2'; [x,y]=solve(eq1,eq2,x,y)%%%%%% TASK 1:- % 4x-32y-40z = 50 % 22y+45x = 10 % -52z+37x+48y = 100%%%%%% TASK 2:- % z = 23*exp(y) + 2^x % x = 3-y^2 % y = 4*x^5clear, syms x y z; eq1='z = 3*exp(y)';eq2='x = 3-z^2'; eq3='y = 4*x^5'; [x,y,z] = solve(eq1,eq2,eq3,x,y,z) double(x),double(y),double(z)%decimal% Audio Signal Processing % WAVPLAY clc; close all; clear all; t = 0:0.0001:0.01; y = 3 * sin(2*pi*100*t); subplot(211); plot(t,y); axis([0 0.01 -6 6]); title('sin wav'); xlabel('Time'); ylabel('Amplitude'); pause; wavplay(y,44100); %%%%%%%%%%%% Listen 'cos' wav sound %%%%%%%%%%%% Listen 'tan' wav sound % WAV WRITE wavwrite(y,44100,'c:\sin.wav'); % WAV Read x = wavread('c:\sin.wav'); % WAV Record x = wavrecord(30000,10000,'init16'); wavplay(x,10000); %%%%%%%%%%%%% % Can b used for speech recognition %%%%%%%%%%%%%