This is a MATLAB question please help and show how the code
This is a MATLAB question please help and show how the code works. Will rate fast.
Revisiting MATLAB anonymous functions and basic logic operations. a) Use both \"polyval\" and an anonymous function to calculate the following: i. 4x^4 + 2x^2 + x - 19 at x = 1, 5, 10, 15 ii. x^2 - 5 at x = 1, 3, 12, 20 iii. 5x^3 + 36^x + 8 at x = -5, -1, 3, 8 b) Using basic logic operators, answer the following questions using MATLAB i. Create two vectors of length 10 of your choice, and label them x and z. ii. Show, element by element, where x and z are equal. iii. Show, element by element, where x is greater than z. iv. Show, element by element, where x is less than or equal to z. v. Find the elements where x is greater than z. vi. Find the elements where z is less than or equal to x. vii. Replace the elements in (vi) with values 10 times their original value in a single operation.Solution
a) polyval and anonymous functions
i)
polynomial = @(x, y) polyval(x, y);
p = [4 0 2 1 -19];
q = [1 5 10 15];
polynomial(p, q)
ans =
-12 2536 40191 202946
ii)
polynomial = @(x, y) polyval(x, y);
p = [1 0 -5];
q = [1 3 12 20];
polynomial(p, q)
ans =
-4 4 139 395
iii)
polynomial = @(x, y) polyval(x, y);
p = [5 0 36 8];
q = [-5 -1 3 8];
polynomial(p, q)
ans =
-797 -33 251 2856
b) Use basic MATLAB operations
i) creating two vectors of length 10
x = 1:10
z = 6:15
x =
1 2 3 4 5 6 7 8 9 10
z =
6 7 8 9 10 11 12 13 14 15
ii) show, element by element where x and z are equal
x = 1:10
z = 6:15
for r = 1:10
if isequal(x(1,r), z(1,r))
disp(x(1,r))
end
end
x =
1 2 3 4 5 6 7 8 9 10
z =
6 7 8 9 10 11 12 13 14 15
No elemets are equal
iii) show element by element where x is greater than z
x = 1:10
z = 6:15
for r = 1:10
if x(1,r) > z(1,r)
disp(x(1,r))
end
end
x =
1 2 3 4 5 6 7 8 9 10
z =
6 7 8 9 10 11 12 13 14 15
x is not greater than z at any index
iv) show element by element where x is less than or equal to z
x = 1:10
z = 6:15
for r = 1:10
if x(1,r) <= z(1,r)
disp(x(1,r))
end
end
x =
1 2 3 4 5 6 7 8 9 10
z =
6 7 8 9 10 11 12 13 14 15
1
2
3
4
5
6
7
8
9
10
v) Find the elements where x is greater than z
x = 1:10
z = 6:15
for r = x
for c = z
if r > c
disp(r)
break;
end
end
end
x =
1 2 3 4 5 6 7 8 9 10
z =
6 7 8 9 10 11 12 13 14 15
7
8
9
10
vi) FInd the elements where x is less than or equal to z
x = 1:10
z = 6:15
for r = x
for c = z
if r <= c
disp(r)
break;
end
end
end
x =
1 2 3 4 5 6 7 8 9 10
z =
6 7 8 9 10 11 12 13 14 15
1
2
3
4
5
6
7
8
9
10
vii) Replace the vi) elements with a value ten times its original value
x = 1:10
z = 6:15
x(1,6) = x(1,6) * 10
z(1,6) = z(1,6) * 10
x =
1 2 3 4 5 6 7 8 9 10
z =
6 7 8 9 10 11 12 13 14 15
x =
1 2 3 4 5 60 7 8 9 10
z =
6 7 8 9 10 110 12 13 14 15



