Sie sind auf Seite 1von 13

OBJECTIVE: Understand what MATLAB is and why it is widely used in engineering and science.

e. To become more familiar with the commands used in MATLAB and also its functions.

EXPERIMENTS: As we know, MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. MATLAB had been used widespread especially in the signal processing field. In this experiment, it started with a basic arithmetic operation and then a logical operation. Through this experiment, we got to learn about its syntax and also its capabilities. It also reminds us about the basic commands in MATLAB such as arithmetic and plotting command.

RESULTS: The executable MATLAB code in the attachment Question 1: Evaluate the following expressions. (a) y=sin(x) Ans: x = 0.7071 rad y = sin(0.7071) = 0.6496 *code refer to fig1(a) (b) convert y in part (a) from radians to degrees Ans: x = [ (20.5)/2 ] . [ 180/pi ] = 40.5138o y = sin(40.5138o) = 0.6496 *code refer to fig1(b) (c) c4 = 6 + j sin (0.5) + c1 Ans: c1 = (-2)0.5 = j1.4142 (using c1 from the example) where x=(20.5)/2

c4 = 6 + j sin (0.5) + j1.4142 = 6 + j1.8936 *code refers to fig1(c) (d) c5 = real part of c4 Ans: c5 = 6 *code refers to fig1(d) Question 2: Edit the command use in question 1(d) to create a variable equal to the imaginary part of c4. Ans: For question 1(d), it asked for the real part of c4. The command is;
>> c5 = real (c4)

Then, we need to change it into imag (c4) as to find the imaginary part of c4. So, we called the previously command by use arrow and then edit it likes below;
>> c5 = imag (c4)

Question 3: Exchange the stem and plot functions in the example and watch the results. Explain the advantages and disadvantages of each function.
>> n1 = [-5:5], n2 = [-5:0.1:5]; >> x1 = n1>= -2 & n1<2; >> x2 = sin (n2); >> subplot(2,1,1), stem (n1,x1) >>subplot(2,1,2), plot (n2,x2)

Ans: As referred to the attachment, fig 3(a) is the graph before exchanging the stem and plot functions. Fig 3(b) is the graph after exchanging the functions.

Question 4: Create and plot the following signals using sampling rate 10Hz. (a) y1 = cos (5t); (b) y2 = 2exp(-2t)*cos(5t); (c) add a noise scaled by 0.2 on y1.

Ans : The full coding for above is in the fig 4. The signal for (a) is fig 4(a), the signal for (b) is fig 4(b) and the signal for (c) is fig 4(c). Question 5: Execute the following and explain what the dot-transpose operator does? Ans:
>> a=[1:5]; >> c=1+i*a c=

Columns 1 through 4 1.0000 + 1.0000i 1.0000 + 2.0000i 1.0000 + 3.0000i 1.0000 + 4.0000i Column 5 1.0000 + 5.0000i >> b=c b=

1.0000 - 1.0000i 1.0000 - 2.0000i 1.0000 - 3.0000i 1.0000 - 4.0000i 1.0000 - 5.0000i >> b = c. b=

1.0000 + 1.0000i 1.0000 + 2.0000i 1.0000 + 3.0000i 1.0000 + 4.0000i 1.0000 + 5.0000i

Question 7: Suppose the sampling rate is 10Hz. (a) Generate a step function u(t-2); (b) Generate a pulse function u(t-2) u(t-6); (c) Generate an impulse function at t=4. Ans : The plotting graph for (a) is fig 7(a), the plotting graph for (b) is fig 7(b), and the plotting graph for (c) is fig 7(c).

Question 8: Add three lines of code to your script, so that it will plot a cosine on top of the sine. Use the hold function to add a plot of
0.5*cos(2*pi*0.789*tt)

*refer to fig8(a) Question 9: Find the mistake in the following function.


Function [sum,prod] = sumprod(x1,x2) %SUMPROD Function to add and multiply two complex numbers %usage: % % % % % % sum = z1+z2; prod = z1*z2; Ans : [sum,prod] = sumprod(x1,x2) x1 = a complex number x2 = another complex number sum = sum of x1 and x2 prod = product of x1 and x2

The mistake is that for the equation sum and prod, the variable that is used is z1 and z2. If we look back at the function definition on the top, the variables used are x1 and x2, so by changing the z1 and z2 to x1 and x2 the function will work.

ANALYSIS: For question 1, it involved a basic command in MATLAB. A simple mathematical problem which is involved the conversion from radian to degree and real and imaginary part of a complex number. For question 2, it asked to create a variable equal to the imaginary part of c4 which is by using command imag (c4). It is the same as finding the real part of a complex number. To do this, the arrow key is used to recall the previous command c5 = real(c4). Then backspace and arrow key are used to edit the command. For question 3, both stem and plot function can be used to plot a signal. But the difference between those functions are stem function is used when to plot a discrete-time signal while plot function is used when to plot a continuous-time signal. This can be seen in the fig..The functions stem and plot are both best suited for different conditions. For stem function, its advantage is that it can clearly show the values at a certain point but it only applies for signals which are discrete-time and short. While plot function's advantage is that it can be used for both continuous and discrete-time signals but it requires a large range of data for the independent variable so that it can obtain smooth curve. For question 4, it is about creating a sampled sinusoidal signal. For that, we need to know how to make an array variable and its basic operation. There is also a new function that we learned are randn function. This function will generate an array specified by the user of randomly generated number by using an algorithm already present in MATLAB. For question 5, as we can see in the result part there is a difference between b=c and b=c. . The first equation finds the conjugate of c and transposes it. While the second equation only transposes c vector. It can be said that what the dot-transpose does is transposes the matrix. For question 7, I found the solution in the internet. It used an inline command. The inline command give an opportunity to create a function of any number of variables by giving a string containing the function followed by a series of strings denoting the order of the input variables. It is much easier to produce a step function. For question 8, it requests to plot two graphs in one layout. By using hold on command, it freezes the current plot, so that an additional plot can be overlaid. Lastly for question 9, is about correcting the coding which contain a mistakes of defined variables.

CONCLUSION After completing this experiment which is an introduction to MATLAB, I realised that there is many things that I did not know and also a lot more to learn about this tool. So far what I knew was the basic commands with involve in simple mathematical problems. The new things that I learned through this experiment are the process of creating a sampled sinusoidal signal and some new functions. As this tool is a tool of choice in academia in most engineering field, I need to practice more about its syntax. It is also good for my future.

ATTACHMENT Question 1

Fig 1(a)

Fig 1(b)

Fig 1(c)

Fig 1(d) Question 2

Fig 2

Question 3

Fig 3(a)

Fig 3(b)

Question 4

Fig 4

Fig 4 (a)

Fig 4(b)

Fig 4(c)

Question 7
>> u=inline('t>=0') >> t=(0:0.01:10);

>> plot(t,u(t-2)) >> axis([0 10 -0.5 1.5]) >> xlabel('t') >> ylabel('u(t-2)')

Fig 7(a)
>> u=inline('t>=0') >> t=(0:0.01:10); >> plot(t,u(t-2)-u(t-6)) >> axis([0 10 -0.5 1.5]) >> xlabel('t') >> ylabel('u(t-2)- u(t-6)')

Fig 7(b)
>> t=(0:1:10); >> a=t==4; >> stem(t,a) >> xlabel('t') >> ylabel('d(t)') >> title('impulse function')

Fig 7 (c)

Question 8
>> tt=(-2:0.05:3); >> y1=sin(2*pi*0.789*tt); >> plot(tt,y1) >> grid on >> xlabel('time(s)') >> hold on >> y2=0.5*cos(2*pi*0.789*tt); >> plot(tt,y2) >> ylabel('y') >> title('graph sine and cosine')

Fig 8(a)

Das könnte Ihnen auch gefallen