Write the mat lab script that assigns the vectors P1 and P2, and adds two polyno
ID: 3110401 • Letter: W
Question
Write the mat lab script that assigns the vectors P1 and P2, and adds two polynomials in vector format named P, where P = P1 + P2, i.e., P1 = 1 - 2x and P2 = -1 + 4x^3. > > ?? Given the set of commands in MATLAB, > > A = [1 -2 4; 2 -5 1;-1 3 0; 2 -2 -1] % first command > > A (2, 2:3) % second command > > A= - 2 *A; A (3, 2)^-1 degree % third command a. What will display on the screen after the first command? b. What will display on the screen after the second command? c. What will display on the screen after the third command? What is the array f displayed after execution of the following commands? > > p = [-1 0 1 2 3); N = length (p); f = p (N:-1:N-1).* [N - 2 N - 3] f = ___Explanation / Answer
Part 1:
MATLAB code:
clear
clc
x = -5:1:5;
disp('x:'), disp(x);
p1 = 1-2*x;
p2 = -1 + 4*(x.^3);
disp('P1:'), disp(p1);
disp('P2:'), disp(p2);
p = p1 + p2;
disp('P:'), disp(p);
Output for Part 1:
x:
-5 -4 -3 -2 -1 0 1 2 3 4 5
P1:
11 9 7 5 3 1 -1 -3 -5 -7 -9
P2:
-501 -257 -109 -33 -5 -1 3 31 107 255 499
P:
-490 -248 -102 -28 -2 0 2 28 102 248 490
>>
Part 2:
Outputs:
>> A = [1 -2 4;2 -5 1;-1 3 0;2 -2 -1]
A =
1 -2 4
2 -5 1
-1 3 0
2 -2 -1
>> A(2,2:3)
ans =
-5 1
>> A = -2*A; A(3,2)^-1
ans =
-0.1667
>>
2(a) A 4x3 matrix is created.
2(b) Accessing columns 2 to 3 (2:3) in row 2.
2(c) Every element in A is multiplied by -2. Then the element in row 3, column 2 is inverted.
Part 3:
Output:
>> p = [-1 0 1 2 3]; N = length(p); f = p(N:-1:N-1).*[N-2 N-3]
f =
9 4
>>
N = length of p = 5.
N:-1:N-1 means take elements from N to N-1, with step -1 (go backward). So we get, [3 2] which is multiplied elementwise by [N-2 N-3] which is [3 2]. So the final answer is [9 4].
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.