Question

In: Computer Science

Write 2 MATLAB functions. One called exptrig which takes a vector of arbitrary length, x, containing...

Write 2 MATLAB functions. One called exptrig which takes a vector of arbitrary length, x, containing any real numbers, and computes the mathematical function

f(x) = 3e^(-x)(cos^2(5x) - sin^2(5x)). The other one will be called trigsum, which takes a vector arbitrary length, x, containing any real numbers, and computes the mathematical function g(x)= ((sin(10x) + cos(10x))/ 2) + 3. Both functions will be computed at each of the values of x, and internally will output these values into a vector, y, of the same dimensions as x.

a) input x1=3 and assign outputs to y1 and z1

b) input a vector x^2, which is the transpose of linspace(-2*pi,2*pi,100), and assign outputs to y2 and z2

c) input a vector x3 = -10:10, and assign outputs y3 and z3

d) input x4= -300, and assign outputs y4 and z4

%sets up a random input vector of integers, of random length

len = randi([1 10],1,1);

x5 = randi([-10 10], 1, len);

y5 = exptrig(x5);

z5 = trigsum(x5)

Solutions

Expert Solution

First funtion.

function fx=exptrig(x)
    
    fx=3.*(exp(-1.*x)).*((cos(5*x)).^2 - (sin(5*x)).^2);
end

Second function

function gx= trigsum(x)
    
    gx= ((sin(10*x) + cos(10*x))./ 2) + 3;
end

Test program

% quesion 1
x1=3
y1=exptrig(x1)
z1=trigsum(x1)

%question2 
x2=linspace(-2*pi,2*pi,100);
x2=(x2)'
y2=exptrig(x2)
z2=trigsum(x2)

% question 3
x3=-10:1:10

y3=exptrig(x3)
z3=trigsum(x3)

% question 4

x4=-300

y4=exptrig(x4)
z4=trigsum(x4)

% question 5

len = randi([1 10],1,1);
x5 = randi([-10 10], 1, len)
y5 = exptrig(x5)
z5 = trigsum(x5)

Simulated result is shown below.

>> test

x1 =

     3


y1 =

    0.0230


z1 =

    2.5831


x2 =

   -6.2832
   -6.1563
   -6.0293
   -5.9024
   -5.7755
   -5.6485
   -5.5216
   -5.3947
   -5.2677
   -5.1408
   -5.0139
   -4.8869
   -4.7600
   -4.6331
   -4.5061
   -4.3792
   -4.2523
   -4.1253
   -3.9984
   -3.8715
   -3.7445
   -3.6176
   -3.4907
   -3.3637
   -3.2368
   -3.1099
   -2.9829
   -2.8560
   -2.7291
   -2.6021
   -2.4752
   -2.3483
   -2.2213
   -2.0944
   -1.9675
   -1.8405
   -1.7136
   -1.5867
   -1.4597
   -1.3328
   -1.2059
   -1.0789
   -0.9520
   -0.8251
   -0.6981
   -0.5712
   -0.4443
   -0.3173
   -0.1904
   -0.0635
    0.0635
    0.1904
    0.3173
    0.4443
    0.5712
    0.6981
    0.8251
    0.9520
    1.0789
    1.2059
    1.3328
    1.4597
    1.5867
    1.7136
    1.8405
    1.9675
    2.0944
    2.2213
    2.3483
    2.4752
    2.6021
    2.7291
    2.8560
    2.9829
    3.1099
    3.2368
    3.3637
    3.4907
    3.6176
    3.7445
    3.8715
    3.9984
    4.1253
    4.2523
    4.3792
    4.5061
    4.6331
    4.7600
    4.8869
    5.0139
    5.1408
    5.2677
    5.3947
    5.5216
    5.6485
    5.7755
    5.9024
    6.0293
    6.1563
    6.2832


y2 =

   1.0e+03 *

    1.6065
    0.4201
   -1.0265
   -0.8629
    0.3451
    0.8499
    0.1768
   -0.5669
   -0.4337
    0.2129
    0.4478
    0.0690
   -0.3113
   -0.2164
    0.1283
    0.2350
    0.0234
   -0.1701
   -0.1071
    0.0759
    0.1228
    0.0053
   -0.0925
   -0.0525
    0.0443
    0.0639
   -0.0009
   -0.0501
   -0.0255
    0.0255
    0.0331
   -0.0025
   -0.0270
   -0.0122
    0.0146
    0.0171
   -0.0024
   -0.0145
   -0.0057
    0.0082
    0.0088
   -0.0018
   -0.0077
   -0.0026
    0.0046
    0.0045
   -0.0012
   -0.0041
   -0.0012
    0.0026
    0.0023
   -0.0008
   -0.0022
   -0.0005
    0.0014
    0.0011
   -0.0005
   -0.0012
   -0.0002
    0.0008
    0.0006
   -0.0003
   -0.0006
   -0.0001
    0.0004
    0.0003
   -0.0002
   -0.0003
   -0.0000
    0.0002
    0.0001
   -0.0001
   -0.0002
   -0.0000
    0.0001
    0.0001
   -0.0001
   -0.0001
    0.0000
    0.0001
    0.0000
   -0.0000
   -0.0000
    0.0000
    0.0000
    0.0000
   -0.0000
   -0.0000
    0.0000
    0.0000
    0.0000
   -0.0000
   -0.0000
    0.0000
    0.0000
    0.0000
   -0.0000
   -0.0000
    0.0000
    0.0000


z2 =

    3.5000
    3.6259
    2.8717
    2.2979
    2.7114
    3.5307
    3.6038
    2.8278
    2.2940
    2.7529
    3.5593
    3.5792
    2.7847
    2.2929
    2.7954
    3.5856
    3.5523
    2.7424
    2.2947
    2.8388
    3.6095
    3.5232
    2.7012
    2.2993
    2.8827
    3.6311
    3.4920
    2.6611
    2.3068
    2.9272
    3.6500
    3.4588
    2.6224
    2.3170
    2.9720
    3.6664
    3.4238
    2.5853
    2.3300
    3.0168
    3.6800
    3.3870
    2.5498
    2.3457
    3.0616
    3.6909
    3.3487
    2.5161
    2.3640
    3.1062
    3.6991
    3.3090
    2.4844
    2.3848
    3.1503
    3.7044
    3.2680
    2.4547
    2.4082
    3.1938
    3.7069
    3.2260
    2.4273
    2.4339
    3.2366
    3.7066
    3.1830
    2.4021
    2.4619
    3.2784
    3.7033
    3.1393
    2.3794
    2.4921
    3.3190
    3.6973
    3.0951
    2.3591
    2.5244
    3.3584
    3.6885
    3.0504
    2.3415
    2.5585
    3.3963
    3.6769
    3.0056
    2.3265
    2.5944
    3.4327
    3.6625
    2.9608
    2.3142
    2.6320
    3.4673
    3.6455
    2.9161
    2.3046
    2.6710
    3.5000


x3 =

   -10    -9    -8    -7    -6    -5    -4    -3    -2    -1     0     1     2     3     4     5     6     7     8     9    10


y3 =

   1.0e+04 *

  Columns 1 through 20

    5.6982   -1.0892   -0.0987    0.2084   -0.1153    0.0430   -0.0109    0.0009    0.0009   -0.0007    0.0003   -0.0001    0.0000    0.0000   -0.0000    0.0000   -0.0000    0.0000   -0.0000   -0.0000

  Column 21

    0.0000


z3 =

  Columns 1 through 20

    3.6843    2.3290    3.4418    2.9297    2.6762    3.6137    2.2940    3.5711    2.7476    2.8525    3.5000    2.3085    3.6605    2.5831    3.0391    3.3513    2.3714    3.7036    2.4479    3.2230

  Column 21

    3.1780


x4 =

  -300


y4 =

 -5.6856e+130


z4 =

    2.4026


x5 =

    10    10


y5 =

   1.0e-03 *

    0.1174    0.1174


z5 =

    3.1780    3.1780

Hope you got the answer. If you still have any doubts regarding this, please try to comment , i will help you.


Related Solutions

Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will take 2 inputs: 1) the first input will be a row vector, c, containing the coefficients of the polynomial, starting with the coefficient of the highest - degree term; 2) the second input will be a scalar, x, which is a real number at which the polynomial will be evaluated. The function's only output, y, will be the scalar value of the polynomial computed...
2. Vector Element Calculation Write a MATLAB script which will randomly generate a 20-element vector with...
2. Vector Element Calculation Write a MATLAB script which will randomly generate a 20-element vector with integer values ranging between −100 to +100. Using LOOP command, determine the number of negative elements in the vector. For example, given A = [15 −6 0 -8 −2 5 4 −10 -3 −5], the number of negative numbers are 6.
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
PWrite a VHDL function that accepts a vector of arbitrary length and integer that specifies the...
PWrite a VHDL function that accepts a vector of arbitrary length and integer that specifies the number of bits the input vector to be rotated to the left and returns another vector. For instance functions accepts two inputs: 0101011 and 3 and it returns 1011010.
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix...
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix of size 2 × 2 and returns back the inverse of that matrix if one exists. If no inverse exists, print back a suitable error message. You can safely assume that we will test your code on square matrices of size 2 × 2 only. You can always verify your answer by using the inbuilt inverse function in MATLAB, however, you cannot use the...
Write a MATLAB function that accepts input vector x and returns the number of local maximums...
Write a MATLAB function that accepts input vector x and returns the number of local maximums of x with value between xmin and xmax. Ask user to input values xmin and xmax at the beginning of the procedure. Use vector x (the vector x from that file consists of 1000 numbers ranging from 0.0044 to 0.67).
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
USING MATLAB Where indicated in the script below, write a function, called myflip, which accepts one...
USING MATLAB Where indicated in the script below, write a function, called myflip, which accepts one vector v (either a column or a row) and outputs the same vector v of the same dimensions, but with the values in reverse order (use MATLAB function flip()). In other words, v will be overwritten by its flipped version. In your function, you may only use length() and floor(). You only need one loop. %this code tests the function, myflip, which you will...
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT