Introduction to Matlab Create a script file with all your answers for the follow
ID: 2085056 • Letter: I
Question
Introduction to Matlab Create a script file with all your answers for the following exercises separated by comments and upload it on D2L. The descriptions are complete and for some problems the nature of the input/output and display options is left to you. You have two lab sessions to complete the Lab. 1. Given the array A = [2 4 1: 6 7: 3 5 9], provide the commands needed to: I. assign the first row of A to a vector called x1 II. assign the last 2 rows of A to an array called y III. compute the sum over the columns of A 2. Give the following commands to create a matrix called F: randn ('seed', 123456789) F = randn (5, 10): I. Compute the minimum and maximum value of each column and assign the results to the elements of a vector called min_value and max_value respectively. II. Compute the mean of each column and assign the results to the elements of a vector called avg. III. Compute the standard deviation of each column and assign the results to the elements of a vector called s. s = squareroot sigma(x - M)^2/n - 1 with X the current element, M the mean and n the number of elements In each case, you can check your results with the built - in functions. 3. Compute the value of pi using the following series: pi^2 - /16 = sigma ^infinity _n = 1 1/(2n - 1)^2 (2n + 1)^2 How many terms are needed to obtain an accuracy of 1e - 12? How accurate is the sum of 100 terms of this series?Explanation / Answer
% Quest 1 , first bit
A = [2 4 1;6 7 2; 3 5 9];
x1=A(1,:);
%here ' : ' is short hand for 1:1:end, so we can use that
%bit 2
y=A(end-1:end,:);
%here since thee question asked for last two rows,
%we start from (end-1)th row till end
%bit 3
sum_of_columns=sum(A,1);
%her sum function calculates sum of array elements
%A is the array & 1 is for dimension i.e. column here.
%Quest. 2
randn('seed',123456789);
F=randn(5,10);
%bit 1
%no of column & rows are kept in c & r
[r c]=size(F);
%this code is for finding the max of each column
%this code comapares first element of column with next one.
%if the nect one is bigger , thats replaced as max element
% so on.
for j=1:c
x=F(1,j);
for i=2:r
if x<F(i,j)
x=F(i,j);
end
end
max_value(1,j)=x;
end
%this code is for finding the max of each column
%This is same as the above code except comaaprison is changed
%in if statement
for j=1:c
x=F(1,j);
for i=2:r
if x>F(i,j)
x=F(i,j);
end
end
min_value(1,j)=x;
end
%bit 2
%This code sums all elements in a column &
%finds average by dividing with row numbers
%n
for j=1:c
sum=0;
for r=1:r
sum=sum+F(r,j);
end
avg(1,j)=sum/r ;
end
%bit 3
for j=1:c
sum=0;
for i=1:r
sum=sum+(F(r,j)-avg(1,j))^2;
end
s(1,j)=sqrt(sum/(r-1));
end
%Question 3
t=0;
sum=0;
value=0;
n=1;
%the program runs unitl accuracy upto 1e-12 is reached
%to run upto 100 times, we can use for loop
while(abs(sum-pi)>1e-12)
value=value+(1/(((2*n-1)^2)*(2*n+1)^2));
sum=sqrt(16*value+8);
n=n+1;
%t gives number of iterations
t=t+1;
end
If any doubt or clarification needed please let me know in the comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.