CSE Exam Hacks



AMITY UNIVERSITY UTTAR PRADESHAMITY SCHOOL OF ENGINEERING AND TECHNOLOGY, NOIDAComputer Science & Engineering2096135240030Experiment 1 Date : 05/01/2016Aim : create a one and two dimensional array then Perfom arithmetic operations : addition , subtraction , multiplaction and exponential.Perfom matrix operations : inverse , transpose ,rank.Software Required : MATLAB 7.0Introduction : Addition: C = A + B adds arrays A and B and returns the result in C.C = plus(A,B) is an alternate way to execute A + B, but is rarely used. It enable ?operator overloading for classes.Subtraction: C = A - B subtracts array B from array A and returns the result in C.C = minus(A,B) is an alternate way to execute A - B, but is rarely used. It enables ??? operator overloading for classes.Multiplication: C = A*B is the matrix product of A and B. If A is an m-by-p and ??? B is a p-by-n matrix, then C is an m-by-n matrix defined byThis definition says that C(i,j) is the inner product of the ith row of A with the jth column of B. You can write this definition using the MATLAB? colon operator asC(i,j) = A(i,:)*B(:,j)For nonscalar A and B, the number of columns of A must equal the number of rows of B. Matrix multiplication is not universally commutative for nonscalar inputs. That is, A*B is typically not equal to B*A. If at least one input is scalar, then A*B is equivalent to A.*B and is commutative.C = mtimes(A,B) is an alternative way to execute A*B, but is rarely used. It enables operator overloading for classes.Exponentiation: Y = expm(X) computes the matrix exponential of X. Although it is not computed this way, if X has a full set of eigenvectors V with corresponding eigenvalues D, then [V,D] = eig(X) andexpm(X) = V*diag(exp(diag(D)))/VUse exp for the element-by-element exponential.Inverse: Y = inv(X) returns the inverse of the square matrix X. A warning message is printed if X is badly scaled or nearly singular.Transpose: B = A.' returns the nonconjugate transpose of A, that is, interchanges the row and column index for each element. If A contains complex elements, then A.' does not affect the sign of the imaginary parts. For example, if A(3,2) is 1+2i and B = A.', then the element B(2,3) is also 1+2i.B = transpose(A) is an alternate way to execute A.' and enables operator overloading for classes.Rank: The rank function provides an estimate of the number of linearly independent rows or columns of a full matrix.k = rank(A) returns the number of singular values of A that are larger than the default tolerance, max(size(A))*eps(norm(A)).k = rank(A,tol) returns the number of singular values of A that are larger than tol.Code :a=3;b=5;sum=a+bsub=a-bmul=a*bexp=a^bResult :sum = 8sub = -2mul = 15exp = 243>>Code:a=[1 6 7 ];transpose(a)a'rank(a)Code:a=[1 6 7 ; 40 15 3];transpose(a)a'rank(a)OUTPUT-9588513271500CODE:a=[3 4 1; 6 7 1; 1 4 2]t=transpose(a)r=rank(a)i=inv(a)OUTPUT:a =???3 ????4 ????1????6 ????7 ????1????1 ????4 ????2t =????3 ????6 ????1????4 ????7 ????4????1 ????1 ????2r =3i =???0.0962 ???0.0769 ??-0.3269??-0.1058 ???0.1154 ???0.2596???0.1635 ??-0.2692 ???0.1442>>Code:a=[1 6 7 ; 40 15 3; 50 3 2];transpose(a)a'rank(a)Code: a=[1 4; 1 10];b=[15 10; 2 3];sum=a+bsub=b-amult=a*b-11239520129500Code:a=[1 4 5; 0 10 1;3 4 1];b=[15 10 8; 2 3 7; 2 7 4];sum=a+bsub=b-amult=a*bEXPERIMENT-2Date-19/01/2016Aim- 1.Perfoming matrix manipulations Concatenation, Indexing ,Sorting, Shifting, Reshaping, Resizing, flipping about an axis.2.Creating arrays and performing, a) Relational operations- <,>,= b) Logical operations- and ,or ,nor,xnor,notTHEORY:PASCAL Pascal matrix SyntaxA = pascal(n)A = pascal(n,1)A = pascal(n,2)DescriptionA = pascal(n) returns the Pascal matrix of order n: a symmetric positive definite matrix with integer entries taken from Pascal's triangle. The inverse of A has integer entries. A = pascal(n,1) returns the lower triangular Cholesky factor (up to the signs of the columns) of the Pascal matrix. It is involutary, that is, it is its own inverse. A = pascal(n,2) returns a transposed and permuted version of pascal(n,1). A is a cube root of the identity matrixRAND Uniformly distributed random numbers and arrays SyntaxY = rand(n)Y = rand(m,n)Y = rand([m n])Y = rand(m,n,p,...)Y = rand([m n p...])Y = rand(size(A))rands = rand('state')DescriptionThe rand function generates arrays of random numbers whose elements are uniformly distributed in the interval (0,1). Y = rand(n) returns an n-by-n matrix of random entries. An error message appears if n is not a scalar. Y = rand(m,n) or Y = rand([m n]) returns an m-by-n matrix of random entries. Y = rand(m,n,p,...) or Y = rand([m n p...]) generates random arrays. Y = rand(size(A)) returns an array of random entries that is the same size as A. rand, by itself, returns a scalar whose value changes each time it's referenced. s = rand('state') returns a 35-element vector containing the current state of the uniform generator.Magic Magic square SyntaxM = magic(n)DescriptionM = magic(n) returns an n-by-n matrix constructed from the integers 1 through n^2 with equal row and column sums. The order n must be a scalar greater than or equal to 3. RemarksA magic square, scaled by its magic sum, is doubly stochastic.Reshape Reshape array SyntaxB = reshape(A,m,n)B = reshape(A,m,n,p,...)B = reshape(A,[m n p ...])B = reshape(A,...,[],...)B = reshape(A,siz)DescriptionB = reshape(A,m,n) returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements. B = reshape(A,m,n,p,...) or B = reshape(A,[m n p ...]) returns an n-dimensional array with the same elements as A but reshaped to have the size m-by-n-by-p-by-... . The product of the specified dimensions, m*n*p*..., must be the same as prod(size(A)). B = reshape(A,...,[],...) calculates the length of the dimension represented by the placeholder [], such that the product of the dimensions equals prod(size(A)). The value of prod(size(A)) must be evenly divisible by the product of the specified dimensions. You can use only one occurence of []. B = reshape(A,siz) returns an n-dimensional array with the same elements as A, but reshaped to siz, a vector representing the dimensions of the reshaped array. The quantity prod(siz) must be the same as prod(size(A)).FLIPLR Flip matrices left-right SyntaxB = fliplr(A)DescriptionB = fliplr(A) returns A with columns flipped in the left-right direction, that is, about a vertical axis. If A is a row vector, then fliplr(A) returns a vector of the same length with the order of its elements reversed. If A is a column vector, then fliplr(A) simply returns A.FLIPUD Flip matrices up-down SyntaxB = flipud(A)DescriptionB = flipud(A) returns A with rows flipped in the up-down direction, that is, about a horizontal axis. If A is a column vector, then flipud(A) returns a vector of the same length with the order of its elements reversed. If A is a row vector, then flipud(A) simply returns A.Concatenation: An alternative to using the [] operator for concatenation are the three functions cat, horzcat, and vertcat. With these functions, you can construct matrices (or multidimensional arrays) along a specified dimension. Either of the following commands accomplish the same task as the command C = [A; B] used in the section on Concatenating Matrices:C = cat(1, A, B); ??????% Concatenate along the first dimensionC = vertcat(A, B); ?????% Concatenate vertically??? Indexing: Indexing into a matrix is a means of selecting a subset of elements from the matrix. MATLAB? has several indexing styles that are not only powerful and flexible, but also readable and expressive. Indexing is a key to the effectiveness of MATLAB at capturing matrix-oriented ideas in understandable computer programs.??? Sorting: B = sort(A) sorts the elements of A in ascending order along the first array dimension whose size does not equal 1.If A is a vector, then sort(A) sorts the vector elements.If A is a matrix, then sort(A) treats the columns of A as vectors and sorts each column.If A is a multidimensional array, then sort(A) operates along the first array dimension whose size does not equal 1, treating the elements as vectors.Shifting: Y = circshift(A,K) circularly shifts the elements in array A by K positions. Specify K as an integer to shift the rows of A, or as a vector of integers to specify the shift amount in each dimension.exampleY = circshift(A,K,dim) circularly shifts the values in array A by K positions along dimension dim. Inputs K and dim must be scalars.Logical Operations: OR- A | B | ... performs a logical OR of all input arrays A, B, etc., and returns an array containing elements set to either logical 1 (true) or logical 0 (false). An element of the output array is set to 1 if any input arrays contain a nonzero element at that same array location. Otherwise, that element is set to 0.??? ??? ??? AND- A & B & ... performs a logical AND of all input arrays A, B, etc., and returns an array containing elements set to either logical 1 (true) or logical 0 (false). An element of the output array is set to 1 if all input arrays contain a nonzero element at that same array location. Otherwise, that element is set to 0.??? ??? ??? NOT- ~A performs a logical NOT of input array A, and returns an array containing elements set to either logical 1 (true) or logical 0 (false). An element of the output array is set to 1 if the input array contains a zero value element at that same array location. Otherwise, that element is setXOR Exclusive or SyntaxC = xor(A,B)DescriptionC = xor(A,B) performs an exclusive OR operation on the corresponding elements of arrays A and B. The resulting element C(i,j,...) is logical true (1) if A(i,j,...) or B(i,j,...), but not both, is nonzero.CODE:A=[2, 3. 4; 5, 1, 2]B=[4, 7, 1 ; 9, 6, 2;4 ,5, 8]C=[A;B]OUTPUT:A = 2 3 4 5 1 2B = 4 7 1 9 6 2 4 5 8C = 2 3 4 5 1 2 4 7 1 9 6 2 4 5 8CODE:A=magic(3)sort(A,'ascend')B=rand(4)sort(B,'descend')C=rand(2,3)sort(C,'ascend')D=pascal(2)sort(D,'descend')OUTPUTA = 8 1 6 3 5 7 4 9 2ans = 3 1 2 4 5 6 8 9 7B = 0.9501 0.8913 0.8214 0.9218 0.2311 0.7621 0.4447 0.7382 0.6068 0.4565 0.6154 0.1763 0.4860 0.0185 0.7919 0.4057ans = 0.9501 0.8913 0.8214 0.9218 0.6068 0.7621 0.7919 0.7382 0.4860 0.4565 0.6154 0.4057 0.2311 0.0185 0.4447 0.1763C = 0.4451 0.4660 0.8462 0.9318 0.4186 0.5252ans = 0.4451 0.4186 0.5252 0.9318 0.4660 0.8462D = 1 1 1 2ans = 1 2 1 1>>Code:A=[2 3 5; 6 8 2; 4 3 2; 1 4 8]r=reshape(A,3,4)t=reshape(A,6,2)rotatematrix=rot90(A)fliplr(A)flipud(A)OUTPUT:A = 2 3 5 6 8 2 4 3 2 1 4 8r = 2 1 3 2 6 3 4 2 4 8 5 8t = 2 3 6 4 4 5 1 2 3 2 8 8rotatematrix = 5 2 2 8 3 8 3 4 2 6 4 1ans = 5 3 2 2 8 6 2 3 4 8 4 1ans = 1 4 8 4 3 2 6 8 2 2 3 5CODE:A=[3 4 5 ; 6 7 8]B=[7 1 2 ; 6 3 0]C=A>BD=A<BE=[A==B]F=or(A,B)G=and(A,B)H=not(A)I=not(or(A,B))J=xor(A,B)OUPUT:A = 3 4 5 6 7 8B = 7 1 2 6 3 0C = 0 1 1 0 1 1D = 1 0 0 0 0 0E = 0 0 0 1 0 0F = 1 1 1 1 1 1G = 1 1 1 1 1 0\H = 0 0 0 0 0 0I = 0 0 0 0 0 0J = 0 0 0 0 0 1EXPERIMENT-3Date-2/02/2016Aim- 1.Generate random sequence numbers and plot them. 2.To calculate sum matrix, cumulative sum matrix.Theory: RANDOM:Syntax :X= random('name',A1,A2,A3,m,n)Description :y = random('name',A1,A2,A3,m,n) returns a matrix of random numbers, where 'name' is a string containing the name of the distribution, and A1, A2, and A3 are matrices of distribution parameters. Depending on the distribution some of the parameters may not be necessary. Vector or matrix inputs must all have the same size. PLOT: Syntax: plot(Y)Description :plot(Y) plots the columns of Y versus their index if Y is a real number. If Y is complex, plot(Y) is equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the imaginary component is ignored.). CUMSUM : Syntax B = cumsum(A)B = cumsum(A,dim)Description :B = cumsum(A) returns the cumulative sum along different dimensions of an array. If A is a vector, cumsum(A) returns a vector containing the cumulative sum of the elements of A. If A is a matrix, cumsum(A) returns a matrix the same size as A containing the cumulative sums for each column of A. If A is a multidimensional array, cumsum(A) works on the first nonsingleton dimension. B = cumsum(A,dim) returns the cumulative sum of the elements along the dimension of A specified by scalar dim. For example, cumsum(A,1) works across the first dimension (the rows).SUM Syntax B = sum(A)Description: B = sum(A) returns sums along different dimensions of an array. If A is a vector, sum(A) returns the sum of the elements. If A is a matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column. If A is a multidimensional array, sum(A) treats the values along the first non-singleton dimension as vectors, returning an array of row vectors.Code:a=rand(4,3)plot(a)cumsum(a,1)cumsum(a,2)sum(a)OUTPUTa = 0.9318 0.5252 0.0196 0.4660 0.2026 0.6813 0.4186 0.6721 0.3795 0.8462 0.8381 0.8318-104775323215Plot(a):ans = 0.9318 0.5252 0.0196 1.3978 0.7278 0.7009 1.8165 1.3999 1.0804 2.6627 2.2381 1.9122ans = 0.9318 1.4570 1.4766 0.4660 0.6686 1.3499 0.4186 1.0908 1.4703 0.8462 1.6843 2.5161ans = 2.6627 2.2381 1.9122Code:b=[ 3 4 6; 7 8 9]plot(b)cumsum(b,1)cumsum(b,2)sum(b)c=magic(4)plot(c)cumsum(c,1)cumsum(c,2)plot(cumsum(c,2))sum(c)Outputb = 3 4 6 7 8 9Plot(b):-152400-438150ans = 3 4 6 10 12 15ans = 3 7 13 7 15 24ans = 10 12 15c = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1ans = 16 2 3 13 21 13 13 21 30 20 19 33 34 34 34 34ans = 16 18 21 34 5 16 26 34 9 16 22 34 4 18 33 34Plot(c):95250269240plot(cumsum(c,2))ans = 34 34 34 34Experiment Date : /0/2016Aim : Software Required : MATLAB 7.0Introduction : Experiment-4Date-09/02/16Aim- (a)Evaluate a given expression and rounding it to the nearest integer value using round ,ceil,floor and fixed functions.(b) Tringnometric functions ex-sin,cos,tan,cosec,cot,sec.(c) Logarthimic function(d) Square rootTOOL USED: MATLAB 7.0 THEORY:.COSCosine of an argument in radians Syntax Y = cos(X)DescriptionThe cos function operates element-wise on arrays. The function's domains and ranges include complex values. All angles are in radians. Y = cos(X) returns the circular cosine for each element of X.TAN:Tangent of an argument in radians SyntaxY = tan(X)DescriptionThe tan function operates element-wise on arrays. The function's domains and ranges include complex values. All angles are in radians. Y = tan(X) returns the circular tangent of each element of X.SIN:Sine of an argument in radians SyntaxY = sin(X)Description The sin function operates element-wise on arrays. The function's domains and ranges include complex values. All angles are in radians. Y = sin(X) returns the circular sine of the elements of X.POWER:Financial time series power Syntaxnewfts = tsobj .^ arraynewfts = array .^tsobjnewfts = tsobj_1 .^ tsobj_2ArgumentstsobjFinancial time series object arrayA scalar value or array with the number of rows equal to the number of dates in tsobj and the number of columns equal to the number of data series in tsobj.tsobj_1, tsobj_2A pair of financial time series objects Descriptionnewfts = tsobj .^ array raises all values in the data series of the financial time series object tsobj element by element to the power indicated by the array value.log :Natural logarithm SyntaxY = log(X)DescriptionThe log function operates element-wise on arrays. Its domain includes complex and negative numbers, which may lead to unexpected results if used unintentionally. Y = log(X) returns the natural logarithm of the elements of X. For complex or negative , where , the complex logarithm is returned. log(z) = log(abs(z)) + i*atan2(y,x)SQRT :Square root SyntaxB = sqrt(X)DescriptionB = sqrt(X) returns the square root of each element of the array X. For the elements of X that are negative or complex, sqrt(X) produces complex results.(a) given expression and rounding it to the nearest integer value using round ,ceil,floor and fixed functions.Code:a=rand(1,3)round(a)floor(a)ceil(a)fix(a)b=5678.934round(b)floor(b)ceil(b)fix(b)OUTPUTa = 0.4565 0.0185 0.8214ans = 0 0 1ans = 0 0 0ans = 1 1 1ans = 0 0 0b = 5.6789e+003ans = 5679ans = 5678ans = 5679ans = 5678(b) Trignometric functions Code:a=8;f=100;t=0:0.1:100 ;x=a*cos(2*pi*f*t);plot(x,t)y=a*sin(2*pi*f*t);figure, plot(t,y)z=a*tan(2*pi*f*t);plot(t,z)j=a*cot(2*pi*f*t);plot(t,j)i=a*sec(2*pi*f*t);plot(t,i)OUTPUTCos3302026543000Sintan317530607000cot -371856049911000sec(c) Logrithmic functionCode:a=90loga=log(a)b=rand(2,3)logb=log(b)c=magic(3)logc=log(c)OUTPUTa = 90loga = 4.4998b = 0.3529 0.0099 0.2028 0.8132 0.1389 0.1987logb = -1.0417 -4.6191 -1.5957 -0.2068 -1.9741 -1.6158c = 8 1 6 3 5 7 4 9 2logc = 2.0794 0 1.7918 1.0986 1.6094 1.9459 1.3863 2.1972 0.6931(d) Sqaure roota=679;sqrt(a)b=magic(3)sqrt(b)OUTPUTans = 26.0576b = 8 1 6 3 5 7 4 9 2ans = 2.8284 1.0000 2.4495 1.7321 2.2361 2.6458 2.0000 3.0000 1.4142EXPERIMENT-5Aim- (a) To create a vector X with elements X=(-1)^(n+1)/(2n-1) and add the 100 elements.(b) Plot x,x^3,exp(x),exp(x^3) for the interval(0-4)THEORY:POWER:Syntax:newfts = tsobj .^ arraynewfts = array .^tsobjArgumentstsobjFinancial time series object arrayA scalar value or array with the number of rows equal to the number of dates in tsobj and the number of columns equal to the number of data series in tsobj.tsobj_1, tsobj_2A pair of financial time series objects Descriptionnewfts = tsobj .^ array raises all values in the data series of the financial time series object tsobj element by element to the power indicated by the array value. EXP : Exponential SyntaxY = exp(X) Description:The exp function is an elementary function that operates element-wise on arrays. Its domain includes complex numbers. Y = exp(X) returns the exponential for each element of X. For complex , it returns the complex exponentiaL.PLOT :Linear 2-D plot Syntaxplot(Y)plot(X1,Y1,...)plot(X1,Y1,LineSpec,...)plot(...,'PropertyName',PropertyValue,...)plot(axes_handle,...)h = plot(...)hlines = plot('v6',...)Descriptionplot(Y) plots the columns of Y versus their index if Y is a real number. If Y is complex, plot(Y) is equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the imaginary component is ignored. plot(X1,Y1,...) plots all lines defined by Xn versus Yn pairs. (a)Code:n=0:100;X=(power(-1,(n+1)))/(2*n-1);a=sum(X)OUTPUTa = -7.4252e-005>>(b) Plot x,x^3,exp(x),exp(x^3) for the interval(0-4)Code:n=0:100;X=(power(-1,(n+1)))/(2*n-1);X=0:4;plot(X)b=exp(X);plot(b)a=power(X,3);plot(a)c=exp(power(X,2));plot(c)OUTPUT139065-17145000-3273425521525500(c) plot exp(x)342902349500(d) plot exp(x^2) EXPERIMENT- 6Date-16/02/16AIM- Generating a sinusodial signal of internal frequency 100hz and plotting with graphical enhancements - title name, (x n y)labeling, adding text, and grid on ,adding legend,adding new text to existing plot.TheoryText Create text object in current axes Syntaxtext(x,y,'string')text(x,y,z,'string')text(...'PropertyName',PropertyValue...)h = text(...)Descriptiontext is the low-level function for creating text graphics objects. Use text to place character strings at specified locations.The statements plot(0:pi/20:2*pi,sin(0:pi/20:2*pi))text(pi,0,' \leftarrow sin(\pi)','FontSize',18)annotate the point at (pi,0) with the string sin()Sin Sine of an argument in radians SyntaxY = sin(X)Description The sin function operates element-wise on arrays. The function's domains and ranges include complex values. All angles are in radians. Y = sin(X) returns the circular sine of the elements of X.xlabel, ylabel, zlabel Label the x-, y-, and z-axis Syntaxxlabel('string')xlabel(fname)xlabel(...,'PropertyName',PropertyValue,...)xlabel(axes_handle,...)h = xlabel(...)ylabel(...)ylabel(axes_handle,...)h = ylabel(...)TITLE Add title to current axes Syntaxtitle('string')title(fname)title(...,'PropertyName',PropertyValue,...)h = title(...)LEGEND Display a legend on graphs Syntaxlegend('string1','string2',...)legend(h,'string1','string2',...)legend(string_matrix)legend(h,string_matrix)GRID Grid lines for two- and three-dimensional plots Syntaxgrid ongrid offgrid minorgridgrid(axes_handle,...)Code-a=1;f=100;t=0:0.001:0.5;x=a*sin(2*pi*t*f);plot(x)xlabel('time')ylabel('Sinewave')title('Sinosoidal Wave plot')grid onlegend('sin(t)')text(100,0,' \leftarrow sin(\pi)','fontsize',18);OUTPUTEXPERIMENT -7Date-23/02/16Aim-Solve for first order diffrential equations using built in functions.TOOL USED – MATLAB 7.0THEORY-DSOLVE Symbolic solution of ordinary differential equations :Syntaxr = dsolve('eq1,eq2,...', 'cond1,cond2,...', 'v')Description:dsolve('eq1,eq2,...', 'cond1,cond2,...', 'v') symbolically solves the ordinary differential equation(s) specified by eq1, eq2,... using v as the independent variable and the boundary and/or initial condition(s) specified by cond1,cond2,.... The default independent variable is t. The letter D denotes differentiation with respect to the independent variable; with the primary default, this is d/dx. A D followed by a digit denotes repeated differentiation. For example, D2 is d2/dx2. Any character immediately following a differentiation operator is a dependent variable If the number of initial conditions specified is less than the number of dependent variables, the resulting solutions will contain the arbitrary constants C1, C2,.... You can also input each equation and/or initial condition as a separate symbolic equation. dsolve accepts up to 12 input arguments. Three different types of output are possible. For one equation and one output, dsolve returns the resulting solution with multiple solutions to a nonlinear equation in a symbolic vector. For several equations and an equal number of outputs, dsolve sorts the results in lexicographic order and assigns them to the outputs.Code:dsolve('Dy = a*x')dsolve('Df = f + sin(t)')dsolve('(Dy)^2 + y^2 = 1','s')dsolve('Dy = a*y', 'y(0) = b')dsolve('D2y = -a^2*y', 'y(0) = 1', 'Dy(pi/a) = 0')dsolve('Dx = y', 'Dy = -x')y = dsolve('(Dy)^2 + y^2 = 1','y(0) = 0')OUTPUT:ans =a*x*t+C1ans =-1/2*cos(t)-1/2*sin(t)+exp(t)*C1ans = 1 -1 sin(s-C1) -sin(s-C1) ans =b*exp(a*t)ans =cos(a*t)ans = x: [1x1 sym] y: [1x1 sym]y = -sin(t) sin(t)>>Single Differential Equationdsolve('Dy=1+y^2')(To specify an initial condition, use)y = dsolve('Dy=1+y^2','y(0)=1')y = dsolve('D2y=cos(2*x)-y','y(0)=1','Dy(0)=0', 'x');simplify(y)OUTPUT:ans =tan(t+C1)y =tan(t+1/4*pi)ans =4/3*cos(x)-2/3*cos(x)^2+1/3 >>Code:Several Differential Equationsy = dsolve('Dy+4*y = exp(-t)', 'y(0) = 1')OUTPUT:y =(1/3*exp(3*t)+2/3)*exp(-4*t)>>EXPERIMENT-8Date:8/03/2016AIM- Write a brief script with a request for an input using input command .Evaluate the function h(t) using if-else statement where, h(t)=(t-10) for 0<t<100 and h(t)=(0.45t+900) for t>100.Exercise:t=5,h=-5t=110,h=949.5THEORY:IF Syntax :if expression statementsendDescription:MATLAB evaluates the expression and, if the evaluation yields a logical true or nonzero result, executes one or more MATLAB commands denoted here as statements. When you are nesting ifs, each if must be paired with a matching end. When using elseif and/or else within an if statement, the general form of the statement is if expression1 statements1elseif expression2 statements2else statements3endINPUT Request user input Syntaxuser_entry = input('prompt')user_entry = input('prompt','s')DescriptionThe response to the input prompt can be any MATLAB expression, which is evaluated using the variables in the current workspace. user_entry = input('prompt') displays prompt as a prompt on the screen, waits for input from the keyboard, and returns the value entered in user_entry. user_entry = input('prompt','s') returns the entered string as a text variable rather than as a variable name or numerical value.CODE:t=input('input the value of t for the function h(t)')if(0<t && t<100) display('value of h') h=(t-10)elseif(t>100) display ('value of h') h=(0.45*t+900) else display('error') endOUTPUT:i)input the value of t for the function h(t)5t = 5value of hh = -5ii) input the value of t for the function h(t)110t =110value of hh = 949.500>>EXPERIMENT-9Date- 15/03/16Aim-Generate the square wave from the sum of sine wave of certain amplititude and frequency.Theory-SIN Sine of an argument in radians SyntaxY = sin(X)Description The sin function operates element-wise on arrays. The function's domains and ranges include complex values. All angles are in radians. Y = sin(X) returns the circular sine of the elements of X.Loop Control for, while, continue, breakWith loop control statements, you can repeatedly execute a block of code, looping back through the block while keeping track of each iteration with an incrementing index variable. Use the for statement to loop a specific number of times. The while statement is more suitable for basing the loop execution on how long a condition continues to be true or false. The continue and break statements give you more control on exiting the loop. forThe for loop executes a statement or group of statements a predetermined number of times. Its syntax is for index = start:increment:end statementsendThe default increment is 1. You can specify any increment, including a negative one. For positive indices, execution terminates when the value of the index exceeds the end value; for negative increments, it terminates when the index is less than the end value.Code:t=0:0.01:10;X=sin(t);plot(t,X)xlabel('time')ylabel('x')title('sinosoidal wave')f=5;w=2*pi*f;t=0:0.0001:1;y=0;for n=1:2:99 //loop for making harmonic function y=y+(1/n)*sin(n*w*t)endplot(t,y)xlabel('x')ylabel('y')OUTPUT: ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download