by useing MATLAB 4 Create the following matrix a Preallocate

by useing MATLAB:

4. Create the following matrix

a) Pre-allocate by setting all elements to zero. Use two sequential for-loops and no if-statements.

b) Pre-allocate by setting all elements to zero. Use two nested for-loops with an if-elseif-statement.

c) Pre-allocate by setting all diagonal elements to one. Use a while-loop with no if-statements.

210 2100 1000

Solution

a) M=[]
for i=0:3
M[i][0:3]=0
end
for i=0:3
M[i][i]=1
M[i][i+1]=-2
end

b) M=[]
for i=0:3
for j=0:3
if(i==j)
M[i][j]=1
elseif(i>j)
M[i][j]=0
elseif(j=i+1)
M[i][j]=-2
end
end
end

c)i=0
M=[]
while(i<4)
M[i][0:3]=0
j==i
M[i][j]=1
M[i][j+1]=-2
end

by useing MATLAB: 4. Create the following matrix a) Pre-allocate by setting all elements to zero. Use two sequential for-loops and no if-statements. b) Pre-allo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site