Write the MATLAB code needed to generate the following: a) An array, A, is gener
ID: 3109748 • Letter: W
Question
Write the MATLAB code needed to generate the following: a) An array, A, is generated with the following command: A = randn {8, 12); Create a new array, B, composed of the last four rows of the 1st, 9th and 10th columns of A. b) Given any positive integer scalar, n, write a script to compute n-factorial, or r!. The final evaluated quantity must be in a variable named nfact. You may use MATLAB built-in functions except the factorial function. c) Create an inline (anonymous) function, fen, of the expression f(x, y) = x^2 e^4y, such that the function accepts two 1-dimensional arrays as inputs (one for x and one for y) and returns a 1-dimensional array as an output. d) Create a 1-dimensional array, x, that contains 40 linearly spaced elements starting at 6 and decreasing to 1. (i.e., times (1) is 6 and times (40) is 1) e) Create an array, R, with 120 rows and 120 columns in which every element is equal to 0.25, except the elements on the main diagonal, which are equal to 3.Explanation / Answer
part(a)
clc;
clear all;
A=randn(8,12)
A(:,[1,8])=A(:,[8,1]);
B=A(5:8,8:10)
part(b)
clc;
clear all;
n=input('the value of n: ');
nfact=1;
for i=1:n
nfact=nfact*i;
end
nfact
part(c)
clc;
clear all;
n=input('enter no of elements of x or y:');
x = zeros(1,n);
for i=1:n
x(i) = input('enter element of x and press enter ');
end
x = x(1,:)
y = zeros(n,1);
for i=1:n
y(i) = input('enter element of y and press enter ');
end
y = y(:,1)
fcnt=x.^2*exp(4*y)
part(d)
clc;
clear all;
h=5/40;
x = zeros(1,40);
x(1)=6
for i=2:40
x(i)=6-i*h
end
x = x(1,:)
part(e)
clc;
clear all;
R=zeros(120,120);
for i=1:120
for j=1:120
R(i,j)=0.25;
end
end
for i=1:120
R(i,i)=3;
end
R=R(:,:)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.