0% found this document useful (0 votes)
10 views6 pages

Lecture 3

This document contains examples of using basic MATLAB functions like polynomials, plotting, matrices, and conditional/loop statements. It shows how to define and manipulate polynomials, plot different graph types, use Cramer's rule and matrices, and demonstrates if/else and for/while loop structures.

Uploaded by

Arshad Ghafoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Lecture 3

This document contains examples of using basic MATLAB functions like polynomials, plotting, matrices, and conditional/loop statements. It shows how to define and manipulate polynomials, plot different graph types, use Cramer's rule and matrices, and demonstrates if/else and for/while loop structures.

Uploaded by

Arshad Ghafoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

%% Basic For MS and Programming tool for Mathematics

%% polynomial value y=2s^2+3s+4 s=1,-3 y=2x2+1


y=[2 0 0 3 0 5];
%y=[2 3 4]
s=1;
value=polyval(y,s)
%% root of polynomial s^2+3s+2=0 a2+2a+1
p=[1 2 1];
%p=[1 3 2];
r=roots(p)
% Exercise s^4+3s^3-15s^2-2s+9=0
%% poly addition a=s^2+2S+1 b=s^3+s+5
a=[0 1 2 1];
b=[1 0 1 5];
c=a+b
%% subtraction
%% poly multiplication x=s+2; y=s^2+4s+8 for three and more any two first
then with third and so on
x=[1 2];
y=[1 4 8];
z=conv(x,y)
%% poly division x=s+2; y=s^2+4s+8 for three and more any two first then
with third and so on
x=[1 2];
y=[1 4 8];
[a, r]=deconv(y,x)
%% display function
A=[1 2; 3 4];
disp(A);
%%

%% 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);

%% pie chart slices


% cement=4 textile=8 software=20 chemical 2 telecom 7 banking 10
industry=[4 8 20 2 7 10];
show=[0 0 0 20 0 0]
pie(industry,show);
legend('Cement','Textile','Software','Chemical','Telecom','Banking')
%% pie chart slices
% cement=4 textile=8 software=20 chemical 2 telecom 7 banking 10
industry=[4 8 20 2 7 10];
show=[0 0 0 1 0 0]
label={'Cement','Textile','Software','Chemical','Telecom','Banking'}
pie3(industry,show,label);

%% 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;

if (x >= minVal) && (x <= maxVal)


disp('Value within specified range.')
elseif (x > maxVal)
disp('Value exceeds maximum value.')
else
disp('Value is below minimum value.')
end
%% WHILE LOOP
i = 1;
sum = 0;
n=150;
%Now here is the while loop:
while i <= n;
sum = sum +13;
i = i + 1;
end
disp('Total:')
% Total:
disp(sum)
%%
syms x y
sol = solve('x^2-y=2','y-2*x=5');
%%
syms x;
b=taylor(sin(x), x, 10);

You might also like