Matlab
Matlab
EXPERIMENT: XXXXXXXXXXXXX
GROUP NUMBER: XX
DATE: XXXXXXX
MODULE: XXXXXXXXXXXXXX
1
ELEC171-172 MATLAB Assignment STUDENT ID=XXX
Question No=1
Code
%Task (A): First of all, we will create a matrix of (4*5)
A= [3 0 1 -2 4;5 5 3 -7 12;4 46 4 14 15;12 3 17 1 31];
%Task (B): Creating a vector P consisting of the elements in the fourth
column of A.
P=A(:,4);
%Task (C): Creating a vector Q consisting of the elements in the second row
of A.
Q=A(2,:);
%Task (D): Creating a [5x2] array B with all elements in the second through
third columns of A.
B=A(:,2:3);
%Task (E): Creating a two component array v in which the two components of v
are the dimensions of A.
V=size(A);
%Task (F): Creating a matrix B = A^3 where the array calculation is done
element-by-element.
B=A.*A.*A;
%Task (G): Creating a matrix C that is equal to A. Copy the last two columns
of C to overwrite the first two columns of C.
C=A;
C(:,1:2)=C(:,4:5);
%Task (G): Displaying the variables A, B, C, p and v in the command window.
display(A)
display(B)
display(C)
display(P)
display(V)
Result
2
ELEC171-172 MATLAB Assignment STUDENT ID=XXX
Question No=2
Part(a)
Code
%First we will define the values of x which would be used in Y expression
3
ELEC171-172 MATLAB Assignment STUDENT ID=XXX
%First values represent the start and last value represent the final value
%Middle value represent the rate of change in the value of x.
x=0.1:0.1:10;
%Then we write the expresion of Y.
%(x.^3) represents X raised to the power of 3.
y=((x.^3)-(3*(x.^2))-(3*x)+5);
%Ploting x on x-axis and y on y-axis
z=plot(x,y);
%Grid is on for better visualization
grid on;
%X and Y axis Labels
xlabel('X');
ylabel('Y');
%Graph title
title('Graph of Y');
%Red line for data ploting
set(z,'color', 'r');
Result
4
ELEC171-172 MATLAB Assignment STUDENT ID=XXX
Part (b)
We can use the function command to carry out the above task. Function
will be equal to the expression of y and then by using the for loop we
can evaluate the above expression for the values of x and get the
required result.
Question No=3
Part (a, b, c)
Code
%syms is Short-cut for constructing symbolic variables
syms z y x
%First we will define the values of x which would be used in Y expression
%First values represent the start and last value represent the final value
%Middle value represent the rate of change in the value of x.
x=-10:0.01:10;
%Then we write the expresion of Y.
%(x.^3) represents X raised to the power of 3.
y=((x.^3)-(3*(x.^2))-(3*x)+5);
%Then we differentiate y using diff command
z=diff(y);
%When more then 1 plot is required on single window then subplot command is
%used
subplot(2,1,1);
%first plot
p1=plot(x,y);
%Grid is on for better visualization
grid on;
%X and Y axis Labels
xlabel('x');
ylabel('y');
%Graph title
title('Grapf of Y expression');
%blue line for data ploting
set(p1, 'color', 'b');
%second plot
subplot(2,1,2);
%To plot the graph both the vectors should be of equal size so the values
%of x are used which are equal to the length of z
p2=plot(x(:,1:length(z)),z);
grid on;
xlabel('x');
ylabel('z');
title('Grapf of differentiation of y ');
set(p2, 'color', 'b');
Result
5
ELEC171-172 MATLAB Assignment STUDENT ID=XXX
Question No=4
Code
%First of all Using the Prompt command we display the string and
%Asked the user to Enter the currency in which he/she wants to converts his
%pounds
prompt1='This Program converts the Pounds into Other Currency \n Enters the
currency=';
%We store the Input (Currency) From the user in str
str=input(prompt1,'s');
%Then using the prompt command we asked the user to
%Enter the number of British Pounds that HE/SHE wants to convert
prompt2='Enter the number of British Pounds that you wants to convert=';
%We strore the input in value variable
value=input(prompt2);
%Then by using the if else command we compare the str string with each
%availale currency
if strcmp(str,'USD')
%After finding out the Reuired currency we multiply Pounds with the new
6
ELEC171-172 MATLAB Assignment STUDENT ID=XXX
Result
7
ELEC171-172 MATLAB Assignment STUDENT ID=XXX