Question

how to write the code correctly in python

The purpose of this question is to calculate the three cubic roots of a complex number. A complex number is of the form a + i


Enter the real part of the complex number: 5. Enter the imaginary part of the complex number: 2. The complex number is (5+2j)


so far, I just have figured this and I'm not too sure how to to do the next steps;


import cmath


a=5

b=2


a=float(input('Enter the real part of the complex number: '))


b=float(input('Enter the imaginary part of the complex number: '))


c= complex(a,b);

print(c)


0 0
Add a comment Improve this question Transcribed image text
Answer #1
% Matlab program to calculate the three cubic roots of a complex number

% input of a and b
a = input(' Enter the real part of the complex number : ');
while(a == 0) % check if a is 0
a = input(' a cannot be 0. Enter the real part of the complex number : ');
end
b = input(' Enter the imaginary part of the complex number : ');
% print the complex number
fprintf(' The complex number is (%g+%gj)',a,b);
theta = atan(b/a); % calculate theta
r = a/cos(theta); % calculate r
% calculate the roots
root1 = (r^(1/3))*((cos(theta/3))+(1i*sin(theta/3)));
root2 = (r^(1/3))*((cos((2*pi+theta)/3))+(1i*sin((2*pi+theta)/3)));
root3 = (r^(1/3))*((cos((4*pi+theta)/3))+(1i*sin((4*pi+theta)/3)));
% print the output
fprintf(' root1 = %f%+fj',real(root1),imag(root1));
fprintf(' root1^3 = %f%+fj',real(root1^3),imag(root1^3));
fprintf(' root2 = %f%+fj',real(root2),imag(root2));
fprintf(' root2^3 = %f%+fj',real(root2^3),imag(root2^3));
fprintf(' root3 = %f%+fj',real(root3),imag(root3));
fprintf(' root3^3 = %f%+fj ',real(root3^3),imag(root3^3));
%end of program


answered by: edge
Add a comment
Know the answer?
Add Answer to:
how to write the code correctly in python
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
  • In python how could I update my code to tell me the number of guesses it...

    In python how could I update my code to tell me the number of guesses it took my to get the correct number here is my working code so far import random number = random.randint(1,100) total_guess = 0 while total_guess<=100: guess = int(input("what is your guess?")) print (guess) total_guess = total_guess + 1 if guess<number: print("Too low") if guess>number: print("Too high") if guess==number: print("Correct!") break

  • Part 1: Python code; rewrite this code in C#, run the program and submit - include...

    Part 1: Python code; rewrite this code in C#, run the program and submit - include comments number= 4 guesscount=0 guess=int(input("Guess a number between 1 and 10: ")) while guess!=number: guesscount=guesscount+1 if guess<number: print("Your guess is too low") elif guess>number: print("Your guess is too high") else: print("You got it!!") guess=int(input("Guess again: ")) print("You figured it out in ",guesscount," guesses") Part 2: C++ code; rewrite the following code in C#. Run the program and submit. - include comments #include <iostream> using...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • Help with Python code. Right now I'm creating a code in Python to create a GUI....

    Help with Python code. Right now I'm creating a code in Python to create a GUI. Here's my code: #All modules are built into Python so there is no need for any installation import tkinter from tkinter import Label from tkinter import Entry def calculatewages(): hours=float(nhours.get()) nsal=float(nwage.get()) wage=nsal*hours labelresult=Label(myGUI,text="Weekly Pay: $ %.2f" % wage).grid(row=7,column=2) return Tk = tkinter.Tk()    myGUI=Tk myGUI.geometry('400x200+100+200') myGUI.title('Pay Calculator') nwage=float() nhours=float() label1=Label(myGUI,text='Enter the number of hours worked for the week').grid(row=1, column=0) label2=Label(myGUI,text='Enter the pay rate').grid(row=2, column=0)...

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • I have to write this in Python and it must run in Python 3. The last...

    I have to write this in Python and it must run in Python 3. The last two pictures are what I have so far please correct me if anything I have written so far is wrong. anything interesting 6.8. Do addition Summary Now you will replace the pass statement inside the addition case of the iflelif ladder with something that actually does addition. Since the only number we have right now is the accumulator, you will need to ask the...

  • Hi I'm trying to write a code for a web server in python with flask. This...

    Hi I'm trying to write a code for a web server in python with flask. This is what I have so far from flask import Flask app = Flask(__name__) @app.route('/') #first endpoint i.e. "http://localhost/" def index(): return 'hello' #return data in string if __name__ == '__main__': app.run(debug=True) After running the code, I'm given a address to the web with the text hello. The problem is that this only works with my computer that is running the code. If I try...

  • The purpose of this question is to calculate the three cubic roots of a complex number....

    The purpose of this question is to calculate the three cubic roots of a complex number. A complex number is of the form a + ib where i is v-1. The magnitude r of a complex number is Vab. The complex number a + ib can be written as r(cos θ + i sin θ). Therefore a -r cose and b rsin0 and b/a (r sin0)/(r cos0) - tane e- arctan(b/a). The 3 cubic roots of a complex number are...

  • Create a class for working with complex numbers. Only 2 private float members are needed, the...

    Create a class for working with complex numbers. Only 2 private float members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class: a. A default constructor that uses default arguments in case no initializers are included in the main. b. Add two complex numbers and store the sum. c. Subtract two complex numbers and store the difference. d. Multiply two complex numbers and store...

  • In python coding please add functions/methods at least 3 of them. Please just adjust code I...

    In python coding please add functions/methods at least 3 of them. Please just adjust code I have provided and update it to include functions/methods. You can rearrange the code, edit or delete parts to add functions/methods. PLEASE DO NOT make a Whole New code. Please use what I have provided. I do not need a whole different code, just edits on what I have provided. THIS is the original question:Design a modular program that asks the user to enter the...

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