0% found this document useful (0 votes)
168 views31 pages

Hina 001 - Compressed

This document contains summaries of operations on scalar variables, complex numbers, vectors, and matrices in MATLAB: 1. Operations on scalar variables include assigning values, arithmetic operations, exponential and logarithmic functions. 2. Operations on complex numbers include defining complex variables, arithmetic operations, and functions that return the real and imaginary components, conjugate, absolute value, and angle. 3. Operations on vectors include defining vectors, arithmetic operations, accessing elements and slices, finding length, logarithmic and exponential functions, random number generation, statistics like mean, variance, minimum and maximum values, sorting, and concatenating vectors. 4. The document provides an example of defining and accessing elements of a matrix in MATLAB.

Uploaded by

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

Hina 001 - Compressed

This document contains summaries of operations on scalar variables, complex numbers, vectors, and matrices in MATLAB: 1. Operations on scalar variables include assigning values, arithmetic operations, exponential and logarithmic functions. 2. Operations on complex numbers include defining complex variables, arithmetic operations, and functions that return the real and imaginary components, conjugate, absolute value, and angle. 3. Operations on vectors include defining vectors, arithmetic operations, accessing elements and slices, finding length, logarithmic and exponential functions, random number generation, statistics like mean, variance, minimum and maximum values, sorting, and concatenating vectors. 4. The document provides an example of defining and accessing elements of a matrix in MATLAB.

Uploaded by

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

1.

A Operation of Scalar Variables


Q. N0. Input Output
1. X Unrecognized function or variable 'X'.
2. X=12 x=

12
3. X=x+2 x=

14
4. Y=x+3 y=

17
5. Y*6 ans =

102
6. X*y-x*3-y ans =

179
7. X=y^2 x=

289
8. Z=sqrt(y) z=

4.1231
9. X=2;y=4; z=x+y z=

6
10. Z=x*y z=

8
11. X=12e6 x=

12000000
12. Clc
13. X=rand x=

0.8147
14. X=rand x=

0.9058
15. Help rand rand Uniformly distributed pseudorandom numbers.
R = rand(N) returns an N-by-N matrix containing
pseudorandom values drawn
from the standard uniform distribution on the open
interval(0,1). rand(M,N)
or rand([M,N]) returns an M-by-N matrix.
rand(M,N,P,...) or
rand([M,N,P,...]) returns an M-by-N-by-P-by-...
array. rand returns a
scalar. rand(SIZE(A)) returns an array the same
size as A.

Note: The size inputs M, N, P, ... should be


nonnegative integers.
Negative integers are treated as 0.

R = rand(..., CLASSNAME) returns an array of


uniform values of the
specified class. CLASSNAME can be 'double' or
'single'.

R = rand(..., 'like', Y) returns an array of uniform


values of the
same class as Y.

The sequence of numbers produced by rand is


determined by the settings of
the uniform random number generator that
underlies rand, RANDI, and RANDN.
Control that shared random number generator using
RNG.

Examples:

Example 1: Generate values from the uniform


distribution on the
interval (a, b).
r = a + (b-a).*rand(100,1);

Example 2: Use the RANDI function, instead of


rand, to generate
integer values from the uniform distribution on
the set 1:100.
r = randi(100,1,5);

Example 3: Reset the random number generator


used by rand, RANDI, and
RANDN to its default startup settings, so that
rand produces the same
random numbers as if you restarted MATLAB.
rng('default')
rand(1,5)

Example 4: Save the settings for the random


number generator used by
rand, RANDI, and RANDN, generate 5 values
from rand, restore the
settings, and repeat those values.
s = rng
u1 = rand(1,5)
rng(s);
u2 = rand(1,5) % contains exactly the same
values as u1

Example 5: Reinitialize the random number


generator used by rand,
RANDI, and RANDN with a seed based on the
current time. rand will
return different values each time you do this.
NOTE: It is usually
not necessary to do this more than once per
MATLAB session.
rng('shuffle');
rand(1,5)

See Replace Discouraged Syntaxes of rand and


randn to use RNG to replace
rand with the 'seed', 'state', or 'twister' inputs.

See also randi, randn, rng, RandStream,


RandStream/rand,
sprand, sprandn, randperm.

Documentation for rand


Other functions named rand

1.B operations of Complex Numbers


Q.NO Input Output
1) I ans =
0.0000 + 1.0000i
2) J ans =
0.0000 + 1.0000i
3) X=1+3*j x=
1.0000 + 3.0000i
4) Y=-2+j; Null
5) Z=x+y z=
-1.0000 + 4.0000i
6) Z=x*y z=
-5.0000 - 5.0000i
7) Real(z) ans =
-5
8) Imag(z) ans =
-5
9) Conj (z) ans =
-5.0000 + 5.0000i
10) Abs(z) ans =
7.0711
11) Angle(z) ans =

-2.3562
12) Who ans x y z
13) Whos Name Size Bytes Class Attributes

ans 1x1 8 double


x 1x1 16 double complex
y 1x1 16 double complex
z 1x1 16 double complex
14) Clear
15) who

1.C operation of Vectors


Q No. Input Output
1. X=2 : 2:10 x=
2 4 6 8 10
2. Y=1:5 y=
1 2 3 4 5
3. Z=x+y z=
3 6 9 12 15
4. Z=x.*y z=

2 8 18 32 50
5. Z=x*y Error using *
Incorrect dimensions for matrix multiplication.
Check that the number of columns in the first
matrix matches the number of rows in the second
matrix. To perform elementwise multiplication,
use '.*'.

Related documentation
6. Z=x./y z=
2 2 2 2 2
7. Z=x/y z=
2.0000
8. 2*y ans =
2 4 6 8 10
9. Z=0:10 z=
0 1 2 3 4 5 6 7 8 9 10
10. Sum(z) ans =
55
11. Y=[2 1 4 -3] Y=
2 1 4 -3
12. Y=[2;1;4;-3] Y=
2
1
4
-3
13. Z=y’ z=
1
2
3
4
5
14. Z(1) ans =
1
15 Z(2) ans =
2
16. Z(1:3) ans =
1
2
3
17. Z(2:4) ans =
2
3
4
18. Length(z) ans =
5
19. X=[2 4 8 16] x=
2 4 8 16
20. Y=log2(x) y=
1 2 3 4
21. Y^2 Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a
power. Check that the matrix is square and the
power is a scalar. To perform elementwise matrix
powers, use '.^'.
22. y.^2 ans =
1 4 9 16
23. Y=rand(1,5) y=
0.8147 0.9058 0.1270 0.9134 0.6324
24. Y=rand(4) y=
0.0975 0.9649 0.4854 0.9157
0.2785 0.1576 0.8003 0.7922
0.5469 0.9706 0.1419 0.9595
0.9575 0.9572 0.4218 0.6557
25. Y=[7 3 -1 2] y=
7 3 -1 2
26. Mean(y) ans =
2.7500
27. Var(y) ans =
10.9167
28. Min(y) ans =
-1
29. Max(y) ans =
7
30. [a b]= min(y) a=
-1
b=
3
31. Sort(y) ans =
-1 2 3 7
32. Y=[y 5] y=
7 3 -1 2 5
33. Z=[y(3:4) x(1:2)] z=
-1 2 2 4

1. D Operation of Matrices
QNo. Input Output
1. X=[3 6 -2 -1; 0 5 2 1;7 -1 4
8]
2. X(2, 1) ans =
0
3. X(2,3) ans =
2
4. X(1, :) ans =
3 6 -2 -1
5. X(:,2) ans =
6
5
-1
6. X(1:2,:) ans =
3 6 -2 -1
0 5 2 1
7. X(:,2:3) ans =
6 -2
5 2
-1 4
8. Y=[1 0 2;3 2 1;2 3 4] y=
1 0 2
3 2 1
2 3 4
9. Y’ ans =
1 3 2
0 2 3
2 1 4
10. Z=zeros(3,4);
11. Y(1,:)=x(2,:) y=
0 5 2 1
0 0 0 0
0 0 0 0
12. Y(2,:)=x(1,:) y=
0 5 2 1
3 6 -2 -1
0 0 0 0
13. Y(3,:)=[1 2 3 4] y=
0 5 2 1
3 6 -2 -1
1 2 3 4
14. Z=x-y z=
3 1 -4 -2
-3 -1 4 2
6 -3 1 4
15. Z=x*y Error using *
Incorrect dimensions for matrix multiplication.
Check that the number of columns in the first
matrix matches the number of rows in the second
matrix. To perform elementwise multiplication,
use '.*'.
16. Z=x*y’ z=
25 50 5
30 25 20
11 -1 49
17. Z=x.*y z=
0 30 -4 -1
0 30 -4 -1
7 -2 12 32
18. Z=x^2 Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a
power. Check that the matrix is square and the
power is a scalar. To perform elementwise matrix
powers, use '.^'.
19. Z=x.^2 z=
9 36 4 1
0 25 4 1
49 1 16 64
20. Z=2.^x z=
8.0000 64.0000 0.2500 0.5000
1.0000 32.0000 4.0000 2.0000
128.0000 0.5000 16.0000 256.0000
21. Z=x.^2+3*y z=
9 51 10 4
9 43 -2 -2
52 7 25 76
22. Max(z) ans =
52 51 25 76
23. [T1 T2]=max(z) T1 =
52 51 25 76
T2 =
3 1 3 3
24. Mean(z) ans =
23.3333 33.6667 11.0000 26.0000
25. Max(mean(z)) ans =
33.6667
26. Max(max(z)) ans =
76
27. Z=rand(4) z=
0.8147 0.6324 0.9575 0.9572
0.9058 0.0975 0.9649 0.4854
0.1270 0.2785 0.1576 0.8003
0.9134 0.5469 0.9706 0.1419
28. X=inv(z) x=
-15.2997 3.0761 14.7235 9.6445
-0.2088 -1.8442 1.0366 1.8711
14.5694 -1.9337 -14.6497 -9.0413
-0.3690 0.5345 1.4378 -0.4008
29. Y=x*z y=
1.0000 -0.0000 0 -0.0000
-0.0000 1.0000 0 0.0000
0 0.0000 1.0000 0.0000
0 -0.0000 -0.0000 1.0000
30. Size(z) ans =
4 4

1.E Plotting Some basic functions.


QNo. Input Output
1. x=0:0.1:10 x=
Columns 1 through 9
0 0.1000 0.2000 0.3000 0.4000 0.5000
0.6000 0.7000 0.8000
Columns 10 through 18
0.9000 1.0000 1.1000 1.2000 1.3000 1.4000
1.5000 1.6000 1.7000
Columns 19 through 27
1.8000 1.9000 2.0000 2.1000 2.2000 2.3000
2.4000 2.5000 2.6000
Columns 28 through 36
2.7000 2.8000 2.9000 3.0000 3.1000 3.2000
3.3000 3.4000 3.5000
Columns 37 through 45
3.6000 3.7000 3.8000 3.9000 4.0000 4.1000
4.2000 4.3000 4.4000
Columns 46 through 54
4.5000 4.6000 4.7000 4.8000 4.9000 5.0000
5.1000 5.2000 5.3000
Columns 55 through 63

5.4000 5.5000 5.6000 5.7000 5.8000 5.9000


6.0000 6.1000 6.2000
Columns 64 through 72
6.3000 6.4000 6.5000 6.6000 6.7000 6.8000
6.9000 7.0000 7.1000
Columns 73 through 81
7.2000 7.3000 7.4000 7.5000 7.6000 7.7000
7.8000 7.9000 8.0000
Columns 82 through 90
8.1000 8.2000 8.3000 8.4000 8.5000 8.6000
8.7000 8.8000 8.9000
Columns 91 through 99
9.0000 9.1000 9.2000 9.3000 9.4000 9.5000
9.6000 9.7000 9.8000
Columns 100 through 101
9.9000 10.0000
2. Y1=sin(x) y1 =
Columns 1 through 9
0 0.0998 0.1987 0.2955 0.3894 0.4794
0.5646 0.6442 0.7174
Columns 10 through 18
0.7833 0.8415 0.8912 0.9320 0.9636 0.9854
0.9975 0.9996 0.9917
Columns 19 through 27
0.9738 0.9463 0.9093 0.8632 0.8085 0.7457
0.6755 0.5985 0.5155
Columns 28 through 36
0.4274 0.3350 0.2392 0.1411 0.0416 -0.0584
-0.1577 -0.2555 -0.3508
Columns 37 through 45
-0.4425 -0.5298 -0.6119 -0.6878 -0.7568 -0.8183
-0.8716 -0.9162 -0.9516
Columns 46 through 54
-0.9775 -0.9937 -0.9999 -0.9962 -0.9825 -0.9589
-0.9258 -0.8835 -0.8323
Columns 55 through 63
-0.7728 -0.7055 -0.6313 -0.5507 -0.4646 -0.3739
-0.2794 -0.1822 -0.0831
Columns 64 through 72
0.0168 0.1165 0.2151 0.3115 0.4048 0.4941
0.5784 0.6570 0.7290

Columns 73 through 81

0.7937 0.8504 0.8987 0.9380 0.9679 0.9882


0.9985 0.9989 0.9894

Columns 82 through 90

0.9699 0.9407 0.9022 0.8546 0.7985 0.7344


0.6630 0.5849 0.5010

Columns 91 through 99

0.4121 0.3191 0.2229 0.1245 0.0248 -0.0752


-0.1743 -0.2718 -0.3665

Columns 100 through 101

-0.4575 -0.5440
3. Y2=cos(x);
4. Plot(x)
5. Plot(y1)

6. Plot(x,y1) %compare to
5

7. Grid
8. Hold on

9. plot(x,y2)

10. y3=exp(-x) y3 =
Columns 1 through 9
1.0000 0.9048 0.8187 0.7408 0.6703 0.6065
0.5488 0.4966 0.4493
Columns 10 through 18
0.4066 0.3679 0.3329 0.3012 0.2725 0.2466
0.2231 0.2019 0.1827
Columns 19 through 27
0.1653 0.1496 0.1353 0.1225 0.1108 0.1003
0.0907 0.0821 0.0743
Columns 28 through 36
0.0672 0.0608 0.0550 0.0498 0.0450 0.0408
0.0369 0.0334 0.0302
Columns 37 through 45
0.0273 0.0247 0.0224 0.0202 0.0183 0.0166
0.0150 0.0136 0.0123
Columns 46 through 54
0.0111 0.0101 0.0091 0.0082 0.0074 0.0067
0.0061 0.0055 0.0050
Columns 55 through 63
0.0045 0.0041 0.0037 0.0033 0.0030 0.0027
0.0025 0.0022 0.0020
Columns 64 through 72
0.0018 0.0017 0.0015 0.0014 0.0012 0.0011
0.0010 0.0009 0.0008
Columns 73 through 81

0.0007 0.0007 0.0006 0.0006 0.0005 0.0005


0.0004 0.0004 0.0003
Columns 82 through 90
0.0003 0.0003 0.0002 0.0002 0.0002 0.0002
0.0002 0.0002 0.0001
Columns 91 through 99
0.0001 0.0001 0.0001 0.0001 0.0001 0.0001
0.0001 0.0001 0.0001
Columns 100 through 101
0.0001 0.0000
11. plot(x,y3,’r’) plot(x,y3,’r’)

Error: Invalid text character. Check for unsupported
symbol, invisible character, or pasting of
Non-ASCII characters.
12. legend(’sin(x)’,’cos(x)’, legend(’sin(x)’,’cos(x)’,’exp(-x)’)
’exp(-x)’) ↑
Error: Invalid text character. Check for unsupported
symbol, invisible character, or pasting of
Non-ASCII characters.
13. axis([-5 15 -3 3])
14. axis([0 10 -2 2])

15. figure figure



Error: Invalid text character. Check for unsupported
symbol, invisible character, or pasting of
non-ASCII characters.
16. subplot(3,1,1)

17. plot(x,y1)
18. subplot(3,1,2)

19. plot(x,y2)

20. subplot(3,1,3)
21. plot(x,y3)

22. semilogy (x,y3)

23. help plot plot Linear plot.


plot(X,Y) plots vector Y versus vector X. If X or Y is a
matrix,
then the vector is plotted versus the rows or columns of
the matrix,
whichever line up. If X is a scalar and Y is a vector,
disconnected
line objects are created and plotted as discrete points
vertically at
X.

plot(Y) plots the columns of Y versus their index.


If Y is complex, plot(Y) is equivalent to
plot(real(Y),imag(Y)).
In all other uses of plot, the imaginary part is ignored.

Various line types, plot symbols and colors may be


obtained with
plot(X,Y,S) where S is a character string made from one
element
from any or all the following 3 columns:

b blue . point - solid


g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram

For example, plot(X,Y,'c+:') plots a cyan dotted line


with a plus
at each data point; plot(X,Y,'bd') plots blue diamond at
each data
point but does not draw any line.

plot(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the


plots defined by
the (X,Y,S) triples, where the X's and Y's are vectors or
matrices
and the S's are strings.

For example, plot(X,Y,'y-',X,Y,'go') plots the data


twice, with a
solid yellow line interpolating green circles at the data
points.

The plot command, if no color is specified, makes


automatic use of
the colors specified by the axes ColorOrder property.
By default,
plot cycles through the colors in the ColorOrder
property. For
monochrome systems, plot cycles over the axes
LineStyleOrder property.

Note that RGB colors in the ColorOrder property may


differ from
similarly-named colors in the (X,Y,S) triples. For
example, the
second axes ColorOrder property is medium green with
RGB [0 .5 0],
while plot(X,Y,'g') plots a green line with RGB [0 1 0].

If you do not specify a marker type, plot uses no


marker.
If you do not specify a line style, plot uses a solid line.

plot(AX,...) plots into the axes with handle AX.

plot returns a column vector of handles to lineseries


objects, one
handle per plotted line.

The X,Y pairs, or X,Y,S triples, can be followed by


parameter/value pairs to specify additional properties
of the lines. For example,
plot(X,Y,'LineWidth',2,'Color',[.6 0 0])
will create a plot with a dark red line width of 2 points.

Example
x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
24. help semilogy semilogy Semi-log scale plot.
semilogy(...) is the same as PLOT(...), except a
logarithmic (base 10) scale is used for the Y-axis.

1.F Boolean operations and plotting graphs over a limited range of the x
axis
QNO. Input Output
1. A=[0 1 2 3 4];
2. A<3 ans =
1×5 logical array
1 1 1 0 0
3. B=(A>2) B=
1×5 logical array
0 0 0 1 1
4. C=([1 1 0 0] & [ C=
1 1 1 0]) 1×4 logical array
1 1 0 0
5. C=([1 1 0 0] | [ 1 C =1×4 logical array
1 1 0]) 1 1 1 0
6. C=∼[1 0 1 0 0] C=∼[1 0 1 0 0]

Error: Invalid text character. Check for unsupported symbol,
invisible character, or pasting of
non-ASCII characters.
7. C=([1 0 1 1 C=
1]==[1 0 1 0 0]) 1×5 logical array
1 1 1 0 0
8. C=([1 0 1 1] C=([1 0 1 1] ∼=[1 0 1 0 0])
∼=[1 0 1 0 0]) ↑
Error: Invalid text character. Check for unsupported symbol,
invisible character, or pasting of
non-ASCII characters.
9. x=0:0.01:10;
10. y=(x<3);
11. figure figure

Error: Invalid text character. Check for unsupported symbol,
invisible character, or pasting of
non-ASCII characters.
12. plot(x,y);axis([0
10 -2 2]);grid;

13. y=(1<x)&(x<4);
14. plot(x,y);axis([0
10 -2 2]);grid;

15. y=1<x<4;
16. plot(x,y);axis([0 plot(x,y);axis([0 10 –2 2]);grid;
10 –2 2]);grid; ↑
Error: Invalid text character. Check for unsupported symbol,
invisible character, or pasting of
non-ASCII characters.

1.2 USING SYMBOLIC MATH

2.A Write down what each of the lines in the following box does and capture
the execution result.
Q No Input Output
1 syms a b c x t No output
2 y=sin(t); No output
3 diff(y) ans =

cos(t)
4 int(y) ans =

-cos(t)
5 int(y, t, 0, pi) ans =

2
6 z=int(xˆ2*exp(- z=int(xˆ2*exp(-x),x,1,3)
x),x,1,3) ↑
Error: Invalid text character. Check for unsupported
symbol, invisible character, or pasting of
non-ASCII characters.
7 double(z) ans =

0.8147 0.6324 0.9575 0.9572


0.9058 0.0975 0.9649 0.4854
0.1270 0.2785 0.1576 0.8003
0.9134 0.5469 0.9706 0.1419
8 limit(sin(t)/t,t,0) ans =

1
9 symsum(xˆ2, x,1,4) symsum(xˆ2, x,1,4)

Error: Invalid text character. Check for unsupported
symbol, invisible character, or pasting of
non-ASCII characters.
10 T=solve(a*xˆ2+b*x+ T=solve(a*xˆ2+b*x+c,x)
c,x) ↑
Error: Invalid text character. Check for unsupported
symbol, invisible character, or pasting of
non-ASCII characters.
11 T2=solve(a*xˆ2+b*x T2=solve(a*xˆ2+b*x+c,b)
+c,b) ↑
Error: Invalid text character. Check for unsupported
symbol, invisible character, or pasting of
non-ASCII characters.
12 a=1;b=2;c=3; No Output
13 z=eval(T) Unrecognized function or variable 'T'.
14 a=t; No output
z=eval(T) Unrecognized function or variable 'T'.
Q No Input Output
4.A function xdB=lin2dB(x) Ans= 20
xdB=10*log10(x);
4.B
fx=1/sqrt(2*pi*v)*exp(-(x-
m).^2/(2*v));
plot(x,fx)
4.C function e = swap(A,row0col1,c,d) function e = swap(A,row0col1,c,d)
e = A; ↑
if row0col1 == 0 Error: Function definition are not supported in this
context. Functions can only be created as local or
e(d,:) = A(c,:); nested functions in code files.
e(c,:) = A(d,:);
end
if row0col1 == 1
e(:,d)=A(:,c);
e(:,c)=A(:,d);
End
4.C2 >>x=rand(4,5) >>x=rand(4,5) >>y=swap(x,0,2,4) >>z=swap(y,1,5,1)
>>y=swap(x,0,2,4) ↑
>>z=swap(y,1,5,1) Invalid use of operator.
Simulink: -

Creating Subsystem
PART B : GUI

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy