ENEL101Assignment 2 - lab 2 of enel 101 with solutions PDF

Title ENEL101Assignment 2 - lab 2 of enel 101 with solutions
Course Computing Tool I
Institution University of Calgary
Pages 10
File Size 392.2 KB
File Type PDF
Total Downloads 76
Total Views 144

Summary

lab 2 of enel 101 with solutions...


Description

3. Assignment 2: Matlab programming using script files Reading assignment: The questions are based on content from chapter 4 and chapter 6 of the textbook“Matlab,Anintroductionwithapplications”. Chapter4isonwritingM-filesandfundamentals of programming. Chapter 6 is on conditional statements and loops. All questions are of equal value. ........................................................................................................ Q1 Consider a set of spheres with volumes given in an array of v = [10, 20, 60,100]. Write a script file that determines the radius of these spheres and place these in an array r. Also determine the area of the sphere and put this into an array of A. (Matlab input) % Sphere Calculation Script File r = ((3*v)/(4*pi)).^(1/3) A = 4*pi*(r.^2)

v = [10,20,60,100]

v=

10 20 60 100

>> SphereCalc

(Matlab Response)

r=

1.3365 1.6839 2.4286 2.8794

A=

22.4466 35.6318 74.1171 104.1879 Q2 Write a script program that generates a 5 by 4 array of random numbers using rand(), then writes these random numbers out as a column of 20 numbers in an ascii file called rand_num.txt using save(). (Matlab input) % rand_num script file x = rand(5,4); Nx = reshape(x,20,1); save('rand_num.txt','Nx','-ascii')

>> Rand_Num

(Matlab Response – open the file rand_num.txt and take a screen shot of it)

................................................... Q3 Write a script program that reads in rand_num.txt, sums up the numbers and assigns this sum to a variable z.

(Matlab input) % script for reading rand_num.txt A = load('rand_num.txt') z = sum(A) save('rand_num.txt','z','-ascii')

>> ReadRandNum

(Matlab Response – open the file rand_num.txt and take a screen shot of it)

Q4 Write a script program that generates a 5 by 4 array of random numbers using rand(), then writes these random numbers out as a column vector of 20 numbers. A second column array contains the square of these numbers. The resulting two columns are then to be written into in an Excel spread sheet using xlswrite(). (Matlab input) % Script for excel sheet x = rand(5,4); Nx = reshape(x,20,1); nx = Nx.^2; C = [Nx,nx]; xlRange = 'A1:B20'; xlswrite('ENEL101#2Q4.xlsx',C,xlRange)

>> ExcelArray (Matlab Response – open the produced excel spreadsheet in Mirosoft Excel and do a screen shot of the two columns of numbers)

ENEL101&2019&Notes&and&assignments&Page&15& Q5 Write a script program that imports the data from the excel spreadsheet that you produced in question 7. Take the squared values of the random numbers in the second column and assign these to a variable C. (Matlab input) % Script for importing excel data Range = 'B1:B20'; C = readtable('ENEL101#2Q4.xlsx','Range','B1:B20')

Q6 Generate a 5 by 5 matrix of random variables called A and a vector that is 5 by 1 called B using rng('default') A = rand(5) B= rand(5,1) Now solve the system of linear equations as Ax=B. Print the solution of x to a file solution_x.txt using fopen() to create the file, fprintf() to write formatted data to the file and then fclose() to close the file. Then open the file with an editor (note pad or matlab editor) and get a screenshot of the file content. (Matlab input) % Script for solving systems of linear equations rng('default'); A = rand(5); B = rand(5, 1); x = linsolve(A,B); fileID = fopen('solution_x.txt','w'); fprintf(fileID,'%d\n', x); fclose(fileID);

>> SolveSystem

(Matlab Response – open the produced txt file in an editor and do a screen shot of the content)

Q7 Write a command that simulates the toss of three dice each with equal chance of producing the outcome of {1,2,3,4,5,6}. Output the sum of the values of three dice. (Matlab input) SumofDi = ((randi([1,6])) + (randi([1,6])) + (randi([1,6])))

SumofDi =

15 Q8 Fibonacci numbers are the numbers in a sequence in which the first two elements are 0 and 1 and the value of each subsequent element is the sum of the previous two elements. Write a Matlab program that generates the first 30 Fibonacci numbers and places them in a column vector called x. Then let y be the sum of all of the elements in x. Output y Matlab input % n x x y

Script for fibonacci sequence = 1:29; = fibonacci(n); = transpose(x); = sum(x)

>> fibcalc

Matlab response y=

1346268 ..............................................................................................................................................................

Q9 Write a Matlab program that determines the value of

ENEL101&2019&Notes&and&assignments& Page&16& Matlab Input % Script for sum of series s = 20; x = 0; for n = -3:s; x = x + (n^3)*(sin(n)); end fprintf('The sum of the series is: %f\n', x)

>> SumofSeries Matlab Response The sum of the series is: 1910.592988 .............................................................................................................................................................. 𝑛& Q10 The nth row of Pascal’s triangle is given by the binomial coefficients of (& )&where m is incremented from 0 to n. Write a program that determines the sum of each row of Pascal’s triangle up to the twentieth row. Place these sums into a column vector of x. Matlab input % Script for sum of pascal rows n = 20; sum = 1; for r = 1:n; sum = sum*2; x(1) = 1; x(r+1) = sum;

end fprintf(' The column vector x is: \n') disp(transpose(x))

>> Pascal

Matlab output The column vector x is: 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536

131072 262144 524288 1048576

Q11 Write a Matlab program that determines the value of &

Matlab input % Script for two sum of series S = 15; s = 20; x = 0; for m = 5:S; for n = -3:s; x = x + (m)*(n^3)*(sin(n+m)); end end fprintf('The sum of the double series is: %f\n', x)

>> DSumofSeries

Matlab output The sum of the double series is: 153747.429344

ENEL101&2019&Notes&and&assignments&

Page&17&...


Similar Free PDFs