In: Computer Science
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)
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.