



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




2. A. Write down what each of the lines in the following box does and capture the execution
result.
syms a b c x t %syms command is used to define variables symbolically used as symbol instead
of numerical variable that we can see after executing it appears as symbols in workspace.
>> y=sin(t)
y=sin(t) %since โtโ is a symbolic variable it returns the same output as input
>> diff(y)
ans=-cos(t) %it takes differentiation of y=sin(t) and returns โ cos(t)
>>int(y,t,0,pi)
ans=2 %it takes integration of y=-cost from t=0 to t=pi.
>>z=int(xห2exp(-x),x,1,3)*
z =5exp(-1) - 17exp(-3) %This command perform following integration โซ
๐
โ๐
๐
๐
>>double(z)
ans=0.9930 %This command converts symbolic expression into double data type
>>limit(sin(t)/t,t,0)
ans=1 %It implement following expression ๐ฅ๐ข๐ฆ
๐โ๐
๐ฌ๐ข๐ง(๐) /๐ by LโHopitalโs rule
symsum(xห2, x,1,4)
ans=3 %This command implement following expression โ ๐
๐ ๐
๐
>> T=solve(ax^2+bx+c,x)**
T=-(b + (b^2 - 4ac)^(1/2))/(2*a) ) %This command find roots by using quadratic formula.
T= - (b - (b^2 - 4ac)^(1/2))/(2*a)
>>T2=solve(axห2+bx+c ,b)**
T2=-(a*x^2 + c)/x %This command separate second argument in command in terms of x
a=1;b=2;c=
c= 3 %This command returns value of c since it is not closed by semicolon.
z=eval(T)
z=-1.000-1.
=-1.000+1.4142 %Eval (short form of Evaluation) command is used for evaluating numeric
roots of symbolic expression
>>a=t; %This expression assign variable โaโ with value of t
>>z=eval(T) %This command evaluate T by replacing t with a and b with 2 and c with c.
z =-((4 - 12t)^(1/2) + 2)/(2t)
3.A-2 Execute the m-file you have created. You can either click 'run' button in the menu bar of the
m-file editor or press the F5 key on the keyboard or type the mfile name in the command window
as
3.A-3 Execute the following commands in the MATLAB command window. Based on the results,
document the meanings of the two variables. Do not capture the execution results.
>>x
After Executing this instruction we get a matrix of order 1x171 in which first entry is 0 second entry
is 0.1 third entry is 0.2 and so on with last entry is 170.
>>y
After executing this instruction we get a matrix of order 8x171 where each row corresponds to each
value of a from 0.1 to 0.8 with difference of 0.1.
3. B Let us write an m-file to plot sine waveforms of 10 different frequencies by properly
modifying the m-file created in 3.A
clear %This command clears the command window
x=0:0.1:17;%we use roll no 317 for this program
for n=1:10%This for loop increment variable n from
1 to 8 with step of 1
a=n/10;%divides every value of n from 1 to 10 by
10
if(a==0.2)%for n=2 this if condition will be true
a=0.25;%replace a=0.2 with a=0.
end
y(n,:)=x.sin(ax);%used to get value of y for
each value of a from 0.1 to 1
end
plot(x,y)%plot command is used for plotting graph
xlabel('x')%used for labeling x-axis
ylabel('y=x sin(ax)')%used for labeling y-axis
%legend is used for differntiating different
waveforms in single plot by
%assigning value of 'a' to each waveform
legend('a=0.1','a=0.25','a=0.3','a=0.4','a=0.5','a
=0.6','a=0.7','a=0.8','a=0.9','a=1.0')
grid on%used for making grid on graph
3.B- 1 :
clear%This command clears variables in workspace
f=10;%f=10Hz is the maximum frequency
T=1/f;%Time period is reciprocal of frequency;
t=-2T:T/10:2T;%time interval
for f=1:10%This for loop increment variable f from
1 to 10 with step of 1
y(f,:)=sin(2pif*t);%used to get value of y for
each value of f from 1 to 10Hz
end%terminate for loop
plot(t,y)%plot command is used for plotting graph
xlabel('t')%used for labeling x-axis
ylabel('y=sin(2pif*t)')%used for labeling y-axis
%legend is used for differntiating different
waveforms in single plot by
%assigning value of 'f' to each waveform
legend('f=1Hz','f=2Hz','f=3Hz','f=4Hz','f=5Hz','f=
6Hz','f=7Hz','f=8Hz','f=9Hz','f=10Hz')
grid on%used for making grid on graph
Execution Result:
4.C-1 An incomplete version of swap.m is provided below. Complete all parts marked by ' ?' and add
a comment for each line you are completing.
function [e] = swap(A,row0col1,c,d)
%This function is used for swaping row or column
of a matrix
%It takes four input argument and return one
swapped matrix
e=A;%equate matrix A equal to e
if(row0col1==0)%check row0col1 is equal to 0 or
not
e(d,:)=A(c,:);%replace row d of matrix e with row
c of matirx A
e(c,:)=A(d,:);%replace row c of matrix e with row
d of matrix A
end%end of if statement
if (row0col1==1)%check row0col1 is equal to 0 or
not
e(:,d)=A(:,c);%replace column d of matrix e with
column c of matirx A
e(:,c)=A(:,d);%replace column c of matrix e with
column d of matrix A
end %end of if statement
x =0.8147 0.6324 0.9575 0.9572 0.
y=0.8147 0.6324 0.9575 0.9572 0.
z= 0.4218 0.6324 0.9575 0.9572 0.