Question

assignment 1a-d in Python. please explain your steps, thanks in advance. This is the only information that we got for this practice exercise, my teacher said that we should try to get the output literally as in the example.
Assignment 1 (a: 1 point; b: 1 point; c: 2 points; d: 2 points) Your goal is to implement functions fac(n), cos1(x, n) and co

So, the main program / test program prints a header line and 21 lines with values for x, cos(x) (use the function in math) an

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#source code:

import math
def fac(n):
   if(n<=0):
       return 1
   else:
       return n*fac(n-1)

print(fac(-1))

def cos1(x,n):
   z=2
   sum=1
   for i in range(0,n):
       if(i%2==0):
           sum=sum-((x**z)/fac(z))
           z=z+2
       else:
           sum=sum+((x**z)/fac(z))
           z=z+2
   return sum
def cos2(x,n):
   return cos1(x,n)
i=0.0
print("x\tcos(x)\tcos1(x,10)\tcos2(x,10)\n")
while(i<=2.0):
   print(str(i)+"\t"+str(math.cos(i))+"\t"+str(cos1(i,10))+"\t"+str(cos2(i,10)))
   i=round(i+0.1,1)
  
      
      

import math def fac(n) : if (n<=0): return 1 else: return n*fac (n-1) print (fac(-1)) def cosl(x,n): z=2 sum=1 for i in range

#output:

cos(x) cos1(x,10) cos2(x,10) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.0 1.0 1.0 0.9950041652780258 0.9800665778412416 0.

#if you have any doubt comment below...

Add a comment
Know the answer?
Add Answer to:
assignment 1a-d in Python. please explain your steps, thanks in advance. This is the only information...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Evaluate the integral for values of x from 0 to 3 in steps of 0.1 AND plot the integral. The prog...

    Evaluate the integral for values of x from 0 to 3 in steps of 0.1 AND plot the integral. The program needs to be written in Python 3. Can only import numpy and matplotlib. CAN NOT use lambda functions or classes of any kind. Exercise 5.3: Consider the integral Plot of the integral from 0 to x 0.8 e: more hints You need to print E(x) with different x values. 0.6 1 Plotting the function will help The calculated values...

  • Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a...

    Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a as a parameter, chooses a starting value for x, and returns an estimate of the square root of a. while True: y = (x + a/x) / 2.0 if y == x: break x = y Part 2 Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the difference between...

  • Please Write in C++ (10-30) @ 11:55pm 1.9 HW7 This homework assignment gives you the opportunity...

    Please Write in C++ (10-30) @ 11:55pm 1.9 HW7 This homework assignment gives you the opportunity to practice functions, functions that call other functions, reference variables, logical statements (what is meant by logical statement is a statement such as if, if/else if, switch), and input validation, HW7 (Graded out of 100) A talent competition has 5 judges, each of whom awards a score between 0 and 10 for each performer. Fractional scores, such as 8.3, are allowed. A performer's final...

  • Lab Exercise #11 Assignment Overview You will work with a partner on this exercise during your la...

    help with cse problem Lab Exercise #11 Assignment Overview You will work with a partner on this exercise during your lab session. Two people should work at one computer. Occasionally switch the person who is typing. Talk to each other about what you are doing and why so that both of you understand each step Part A: Class Date . Download the files for this laboratory exercise, then run the Python shell and enter the following commands: >>>import date >>help(...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • use python IDEL Please highlight the answer 1 ווCIוסטIT Errors and Exceptions. There are two main...

    use python IDEL Please highlight the answer 1 ווCIוסטIT Errors and Exceptions. There are two main types of errors: syntax errors and runtime errors. Syntax errors are detected by the Python interpreter before the program execution during the parsing stage (parsing is a process, during which the Python interpreter analyzes the grammatical structure of the program). Runtime errors are errors in a program that are not detected by the Python interpreter during its parsing stage. They are further divided into...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • use python IDEL Please highlight the answer Problem 1 Errors and Exceptions. There are two main...

    use python IDEL Please highlight the answer Problem 1 Errors and Exceptions. There are two main types of errors: syntax errors and runtime errors. Syntax errors are detected by the Python interpreter before the program execution during the parsing stage (parsing is a process, during which the Python interpreter analyzes the grammatical structure of the program). Runtime errors are errors in a program that are not detected by the Python interpreter during its parsing stage. They are further divided into...

  • Using Python programming language to solve this thanks!!! An engineer has derived a relationship between the...

    Using Python programming language to solve this thanks!!! An engineer has derived a relationship between the force applied to a material and the extension in length that the force would cause. The relationship between force f and extension e is given by: e = { 5.5f if 0 < f < 10 f2 – 10f + 55 if 10 < f < 20 The aim of this task is to specify a list of values which are the values of...

  • please use python thanks will rate!! x + Run C Code Validate Implement the function step_random_walk_20(x_coords,...

    please use python thanks will rate!! x + Run C Code Validate Implement the function step_random_walk_20(x_coords, Y_coords) below, which should take two arrays of equal length containing the x-andy- coordinates for some number of particles. We'll use a very simple random walk algorithm • For each particle, choose a random angle between 0 and 2 • The particle moves by 1 unit of distance in the direction given by d.o. It is displaced by (Ax, Ay) (cos, sino). We'll do...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT