Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a vector x sin(1pi*t+9) for 10,000 evenly spaced points between 0 and

ID: 3599515 • Letter: 1

Question

1. Create a vector x sin(1pi*t+9) for 10,000 evenly spaced points between 0 and 100. Write a 'for' loop that does the following: a. Displays the value of the iterator within each iteration using the fprintf command. b. Check to see if the jth entry of x is bigger than .5 i. If it is, add 1 to a counter. c. Otherwise, check if the jth entry of the counter is smaller than -2 i. If it is, add 1 to a different counter Finally, think of a way to achieve the same counts without writing a for loop. We leaned how to do this last week using logicals. Use the timing functions tic and toc to see the difference in timing 2. Edmund Halley (the astronomer famous for discovering Halley's comet) invented a fast algorithm for computing the square root of a number, A. Halley's algorithm approximates VA as follows: Start with an initial guess: xi. The new approximation is then given by: (15-%(10-3% )) These two calculations are repeated until some convergence is met: However, if you have a bad guess, or a extremely high convergence, it could take quite a while to converge. Therefore, make a maximum number of iterations allowable within the while loop before quitting it, and returning a value of'as the output value Create a function called my_sqrt that approximates the square root of a number It should have three inputs: 1 the number you are trying to find the square root of (A) 2. the initial guess (%) 3. the convergence criteria (&) 4. the maximum number of iterations allowed before giving up (maxit) Your function will return the approximation for the square root of A. You must use a while loop to get full credit Test your script by: i. Approximating the square root of 5 (A-5) ii. Comparing it to the value calculated with the built-in MATLAB function, sqrt. iii. Use format long to compare results. iv. Start with an initial guess, x= 2 v. Use a convergence criteria, 0.001

Explanation / Answer

package com;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;

public class Graph extends JPanel {
public Graph() {
setSize(500, 500);
}

@Override
public void paintComponent(Graphics g) {
Graphics2D gr = (Graphics2D) g; // This is if you want to use Graphics2D
// Now do the drawing here
ArrayList<Integer> scores = new ArrayList<Integer>(10);

Random r = new Random();

for (int i : scores) {
i = r.nextInt(20);
System.out.println(r);
}

int y1;
int y2;

for (int i = 0; i < scores.size() - 1; i++) {
y1 = (scores.get(i)) * 10;
y2 = (scores.get(i + 1)) * 10;
gr.drawLine(i * 10, y1, (i + 1) * 10, y2);
}
}
}