Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please do both questions in python language (jupyter notebook) Exercise 1 Define a python function named f that calculates 1 where x is the required

image text in transcribed

image text in transcribed

image text in transcribed

Please do both questions in python language (jupyter notebook)

Exercise 1 Define a python function named f that calculates 1 where x is the required function parameter. 1+x (32]: x = float eq = float def f (x, eq): x = int(input("Please enter a number")) eq = 1/(1+x*x) print (e) f(x, eq) Please enter a number 5 0.038461538461538464 Use 3 differenct techniques to construct a list (xist) of x values (floats) from 0.0 thru 5.0 by 0.5 steps. [1]: xist = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0] print (xlst) [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.2] [2]: xlst = 0 for i in range(0,10): xlst = xlst+0.5 i=1+1 print (xlst) 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.5 5.0 Given your list of x values, use python to generate a corresponding list of y values (ylst) produced by the f function for each x in your xlst. [] Use matplot lib to visualize the function by plotting a line plot for values Hint: see chapter 5 and Mar03 class notes 22]: # your plot here Now. Define a python function to approximate the area under the curve defined by any given function by adding up a series of small rectangles. As shown in the image below. The width of each rectangle is Ax your xlst step size = 0.5) derived from the interval of concern and the number of rectangles. The height of each rectangle is the value your f function produces for the current x (the left size of the rectangular area). Image Name your function named approx Define your function to accept 4 parameters: the function to use that defines your curve. f above is an example. the starting x value of the interval the ending x value of the interval the number of rectangles (n) to use. Optional with default value of 10 For example, I should be able to call your function like this: approx/.0.0, 5.0, n = 10) Note: your function should sum up then imaginary rectangle areas that fit between the start and end x interval. The final answer will be that sum. Exercise 10 Sally invited 17 guests to a dance party at her estate in the Hamptons. She assigned each guest a number from 2 to 18, keeping 1 for herself. At one point in the evening when everyone was dancing, Sally noticed the sum of each couple's numbers was a perfect square. Everyone was wearing their numbers on their clothing. The question is, what was the number of Sally's partner? Here's a reminder: a perfect square is attained by squaring, or multiplying by itself, an integer. So four is a perfect square of two. Nine is a perfect square of three. Sixteen is a perfect square of four. To identify a perfect square you should define a function that returns True if the parameter n is perfect or False otherwise. One approach is to calculate the square root of the number, then truncate that to an integer and square the resulting integer. If the final answer is the same as the original number (n) then n is a perfect square. With your function, we can derive all of the perfect numbers within a specific range. For example, the perfect squares between 2 than 30 would be 4, 9, 16, 25. Back to our dancing guests.... The dancing couple's numbers are adding up to either 4, 9, 16, 25, etc. And the question is, with the information you have available to you, what's the number of Sally's partner? Use python to calculate. Can you show all dancing couple pairs? [21]: ## your function definition here [ ]: ## your tests here [ ]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions