Lecture 3
Lecture 3
%% multiplication
syms s
a=s+2;
b=s^2+4*s+8;
c=expand(a*b);
disp(c)
%%
syms s
a=s+2;
b=s^2+4*s+8;
c=a+b
%%
syms x
a=x+2;
b=x^2+4*x+8;
c=a-b
%% Cramer's rule
A=[2 4 5 6 9; 2 8 1 2 3;2 9 7 8 9;5 4 2 8 9;5 4 1 9 9];
b=[4;6;8;9;5];
if det(A)==0; error('Matrix is invalid');end
for g=1:length(b);
B=A;
B(:,g)=b;
x(g)=det(B)/det(A);
end
disp(x)
%%
syms s
a=s+2;
b=s^2+4*s+8;
c=simplify(a*b);
%% Bars program to show bar function
x=[1 2 3 4 5 6];
y=[3 5 6 2 3 2];
bar(x,y,'g');
xlabel('Over');
ylabel('Score');
title('waseem vs lara')
%% plot bar graph to show the comparison of average temp in three cities
with different month
temp=[31 28 24 ; 29 26 22;28 25 20;27 24 16;26 22 17;29 25 20];
bar(temp,'group');
xlabel('Months')
ylabel('Temp in celsius');
legend('City A','City B','City C');
title('Temperature of different cites')
%% Bar h program to show bar function
x=[0 1 2 3 4 5 6];
y=[10 15 25 20 30 27 19];
barh(x,y,'c');
xlabel('x-axis');
ylabel('y-axis');
title('graph to show bar function')
%% histogram will make 10 by default bin
x=randn(100,1)*pi;
hist(x);
%% histogram will make 15 bin
x=randn(200,1);
hist(x,15);
%% histogram will make 15 bin
x=randn(200,1);
p=-2:0.5:2;
hist(x,p);
%% pie chart
% cement=4 textile=8 software=20 chemical 2 telecom 7 banking 10
industry=[4 8 20 2 7 10];
pie(industry);
%% cramer rule
A=[3 2 1;-1 4 -6;2 6 7];
B=[5;10;20];
D=det(A);
A1=[B A(:,2:3)];
A2=[A(:,1) B A(:,3)];
A3=[A(:,1:2) B];
x1=det(A1)/D
x2=det(A2)/D
x3=det(A3)/D
%%
A1=[3 2 1;-1 4 -6;2 6 7];
B=[5;10;20];
X=inv(A1)*B
%% 5-3-2017
%% general for for loop
for index = j:k
statements
end
%%
for x = 1:0.5:4
y=2*x+3;
disp( [x y] )
end
%%
for index = j:m:k
statements
end
%% factorial
n = 4;
fact = 1;
for k = 1:n
fact=k*fact;
disp( [k fact] )
end
%% seri\es
a = 10;
x=1;
k = 5;
for n = 1:k
x=a*x/n;
disp( [n x] )
end
%% time command
t0 = clock;
n = 1:10;
s=sum(n);
to=(etime(clock, t0));
%% sum the series 1-1/2+1/3-1/4....
sign = -1;
s=0;
for n = 1:4
sign = -sign;
s=s+sign / n;
disp( [n s] )
end
%%
for ii=0:10
n_factorial=factorial(ii);
fprintf ('%d!=%d\n',ii,n_factorial);
end
%% Basic for if statement
if condition statement, end
%% if statement decision statement
r = rand
if r > 0.5 disp( 'greater indeed' ), end
%%
a = 212;
b = 30;
if (a>b)
j = -1
end
%%
a = 13;
b = 14;
if (a<b)
j = -1
else if (a>b)
j = 2
end
end
%%
a = 14;
b = 4;
if (a<b)
j = -1;
else if (a>b)
j = 2
else
j = 3
end
end
%%
nrows = 10;
ncols = 10;
A = ones(nrows,ncols);
for c = 1:ncols
for r = 1:nrows
if r == c
A(r,c) = 2
elseif abs(r-c) == 1
A(r,c) = -1
else
A(r,c) = 0
end
end
end
%%
% nrows = 10;
% ncols = 10;
% A = ones(nrows,ncols);
% i=1:nrows;
% j=1:ncols
% A(i,j)=2
% A(i+1,j+1)=3
% A(i-1,j-1)=3
%%
limit = 0.75;
A = rand(10,1);
if any(A > limit)
disp('There is at least one value above the limit.')
else
disp('All values are below the limit.')
end
%%
x = 5;
minVal = 2;
maxVal = 6;