{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

MATLAB comp progHW4

# MATLAB comp progHW4 - 4.2(30 points Use a for-end loop in a...

This preview shows pages 1–2. Sign up to view the full content.

Fall 2010, Dr. Frank Shih CS 101 Computer Programming & Problem Solving Homework #4 DUE: 11:55pm, October 21, 2010 NAME: STUDENT ID#: NOTES: Submit the homework solution in Microsoft Word format to http://moodle.njit.edu/ before the above deadline. Absolutely, no late submission is accepted. Write the answers in your own words individually. Any plagiarism will post a “ZERO” score or cause a “FAIL” grade. 4.1 (40 points) Use for-end loops to create a 4×7 matrix in which the value of each element is the sum of its squared indices (the row number and column number of the element). For example, the value of element A(2,5) is 29. Show the script file and the result of the matrix A. Answer: Script File : for x=1:4; for y=1:7; z=(x^2)+(y^2); A(x,y)=z; end end Command Window: >> for_end A = 2 5 10 17 26 37 50 5 8 13 20 29 40 53 10 13 18 25 34 45 58 17 20 25 32 41 52 65

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 4.2 (30 points) Use a for-end loop in a script file to calculate the sum of the first n terms of the series: 1 ( 1) 2 k n k k k =-∑ . Show the script file and the two results of n = 4 and n = 20. Answer: Script File : n=input( 'Enter endpoint of series:' ); B=0; for x=1:n; y=(((-1)^x)*x)/(2^x); Fall 2010, Dr. Frank Shih B=B+z; end Command Window: >> hw4.2 Enter endpoint of series:4 B =-0.1250 >> hw4.2 Enter endpoint of series:20 B =-0.2222 4.3 (30 points) Repeat the previous question 4.2, but using the while-end loop. Show the script file and the two results of n = 5 and n = 10. Answer: Script File : n=input( 'Enter endpoint of series:' ); C=0; x=1; while x<=n; y=(((-1)^x)*x)/(2^x); x=x+1; C=C+y; end Command Window: >> hw4.3 Enter endpoint of series:5 C =-0.2812 >> hw4.3 Enter endpoint of series:10 C =-0.2188...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

MATLAB comp progHW4 - 4.2(30 points Use a for-end loop in a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online