Question
Compute the following problems using Math Lab. Instructions: Answer All Questions using MATLAB commands. Question 1....
Compute the following problems using Math Lab.
Answers
>> % question 1
>> even_whole = 31+1:2:75even_whole =
Columns 1 through 11
32 34 36 38 40 42 44 46 48 50 52
Columns 12 through 22
54 56 58 60 62 64 66 68 70 72 74
>> % question 2
>> x = [2 5 1 6];
>> % a
>> x+16ans =
18 21 17 22
>> % b
x = [2 5 1 6];
x(1)=x(1)+3;
x(3)=x(3)+3;
xx =
5 5 4 6
>> % c
x = [2 5 1 6];
sqrt(x)ans =
1.4142 2.2361 1.0000 2.4495
>> % d
>> x.^2ans =
4 25 1 36
% question 3
>> x = [3 2 6 8]' , y = [4 1 3 5]'x =
3
2
6
8
y =4
1
3
5>> sum(x)+ sum(y)
ans =
32
>> % b
>> x.^yans =
81
2
216
32768>> % c
>> y./xans =
1.3333
0.5000
0.5000
0.6250>> % d
>> x.*yans =
12
2
18
40>> z=x.*y;
>> % e
>> x=sum(z)x =
72
>> w=sum(z);
>> x = [3 2 6 8]'x =
3
2
6
8>> % f
>> x'*y-sum(z)ans =
0
>> % question 4
>> % a
>> 2/2*3ans =
3
>> 6-2/5+7^2-1
ans =
53.6000
>> 10/2\5-3+2*4
ans =
6
>> 3^2/4
ans =
2.2500
>> 3^2^2
ans =
81
>> 2+round(6/9+3*2)/2-3
ans =
2.5000
>> 2+floor(6/9+3*2)/2-3
ans =
2
>> 2+ceil(6/9+3*2)/2-3
ans =
2.5000
>> % question 5
>> x = 2:2:20 % insteal of 20 one could put bigger numberx =
2 4 6 8 10 12 14 16 18 20
>> % b
>> clear x
>> x = 10:-2:-4x =
10 8 6 4 2 0 -2 -4
>> %c
>> clear x
>> x = 1:20;
>> x = 1./xx =
Columns 1 through 6
1.0000 0.5000 0.3333 0.2500 0.2000 0.1667
Columns 7 through 12
0.1429 0.1250 0.1111 0.1000 0.0909 0.0833
Columns 13 through 18
0.0769 0.0714 0.0667 0.0625 0.0588 0.0556
Columns 19 through 20
0.0526 0.0500
>> % d
>> clear x
>> x = 0:20;
>> y = x+1;
>> x = x./yx =
Columns 1 through 6
0 0.5000 0.6667 0.7500 0.8000 0.8333
Columns 7 through 12
0.8571 0.8750 0.8889 0.9000 0.9091 0.9167
Columns 13 through 18
0.9231 0.9286 0.9333 0.9375 0.9412 0.9444
Columns 19 through 21
0.9474 0.9500 0.9524
>> % question 6
>> xn = @(n) (-1)^(n+1)/(2*n-1);
>> xnxn =
function_handle with value:
@(n)(-1)^(n+1)/(2*n-1)
>> % question 7
>> % function handle for hypoteneous
>> f = @(a,b) sqrt(a.^2,b.^2);
>> ff =
function_handle with value:
@(a,b)sqrt(a.^2,b.^2)
>> for i=1:100 x(i)=xn(i); end
sum(x)ans =
0.7829
>> % question 7
>> % function handle for hypoteneous
>> f = @(a,b) sqrt(a.^2+b.^2);
>> ff =
function_handle with value:
@(a,b)sqrt(a.^2+b.^2)
>> % function handle for cosine rule
>> g = @(a,b,t) a.^2 + b.^2 -2.*b.*a.*cos(t);
>> gg =
function_handle with value:
@(a,b,t)a.^2+b.^2-2.*b.*a.*cos(t)
% question 9
x=linspace(0,4);
>> subplot(2,2,1); plot(x,x,'r--'); title('x');...
subplot(2,2,2); plot(x,x.^3,'k--'); title('x^3');...
subplot(2,2,3); plot(x,exp(x),'c-.'); title('exp(x)');...
subplot(2,2,4); plot(x,exp(x.^2),'g--'); title('exp(x^2)');
>> % question 10
>> x = linspace(0.01,0.1,1000); % linspace divide interval into 1000 equal segments
>> plot(x,sin(1./x))
80 60 40 20 exp(x) 0 exp(x2) 60 10 106 40 20 00.8 0.6 0.4 0.2 0 -0.2 0.4 -0.6 -0.8 -1 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11