Thread has been deleted

Last comment
I need help for a few things, the halving method and newtons method
As an example lets say I get the equation
x - 3 = 0
how do I solve that by using the halving method
and how do I solve the same equation using the newtons method?
I think I came to the answer 1, 6666 using the newtons method but I think it could be wrong, and I didnt manage how to do it with the halving method.
Could someone try to do it using python and paste it in this thread. Thanks in advance

And also if u have time:
Equation set: f(x) = x2 + x-3 and g(x) = 2x2 - 2x-3
find the answer to that equation set when: g(x) = f(x)

5 replies

X2? Do you mean x^2?

4 replies

ye. Can u do that one in python?

3 replies

It's a standard way of writing powers of.

2 replies

print('Hello World')

10 replies

I am not that big of a noob xd

+1

+1
works! +rep

+1

+1

+1 new einstein

it works! thanks bro!

+1

+1

+1

anyone that can help?

Imagine asking on hltv instead of stackoverflow

3 replies

rather ask on hltv than stackshitflow

tbf stackoverflow is hella toxic and full of even worse socially awkward individuals than HLTV

1 reply

+1. Plus mods always change people's comments without their consent. Bullshit practice

def newton(f,Df,x0,epsilon,max_iter):
'''Approximate solution of f(x)=0 by Newton's method.
Parameters
----------
f : function
Function for which we are searching for a solution f(x)=0.
Df : function
Derivative of f(x).
x0 : number
Initial guess for a solution f(x)=0.
epsilon : number
Stopping criteria is abs(f(x)) < epsilon.
max_iter : integer
Maximum number of iterations of Newton's method.
Returns
-------
xn : number
Implement Newton's method: compute the linear approximation
of f(x) at xn and find x intercept by the formula
x = xn - f(xn)/Df(xn)
Continue until abs(f(xn)) < epsilon and return xn.
If Df(xn) == 0, return None. If the number of iterations
exceeds max_iter, then return None.
Examples
--------
>>> f = lambda x: x**2 - x - 1
>>> Df = lambda x: 2*x - 1
>>> newton(f,Df,1,1e-8,10)
Found solution after 5 iterations.
1.618033988749989
'''
xn = x0
for n in range(0,max_iter):
fxn = f(xn)
if abs(fxn) < epsilon:
print('Found solution after',n,'iterations.')
return xn
Dfxn = Df(xn)
if Dfxn == 0:
print('Zero derivative. No solution found.')
return None
xn = xn - fxn/Dfxn
print('Exceeded maximum iterations. No solution found.')
return None

Have u tried using a calculator? :()

x-3=0
x=3
Solved

8 replies

yes but in python

4 replies

x = 0 + 3

Are you dumb?
x-3=0 |+3
x=0+3 (0+3=3)
x=3

2 replies

'Are you dumb?' Bro he's just asking, everyone starts somewhere with coding.

1 reply

I was baiting lmao

genius xd

1 reply

thanks

n1

I'm a python expert but this is math and i failed math few times in uni so noppers

2 replies

rip

Me too but i only know this equation with apples

send ur code

i am a python expert in rust what do you need help with

python is cringe
c++ superior

24 replies

50-year-old ex card programmer right there

21 replies

nt python is really slow
c++ better and best

20 replies

#23, expected from lithuanian also

16 replies

nt pole living in vilnius

15 replies

mens mens mens imagine living in rural lithuania xD

14 replies

i live in klaipeda and its fine 🤡

13 replies

yes, rural lithuania
btw do you enjoy the smell all the time?

12 replies

🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡
what smell

11 replies

idk whenever i come to klaipeda theres this piss smell everywhere in the city
u prob already used to it, np mens

10 replies

there is piss smell in beaches in summer when everyone from vilnius comes🤡

9 replies

imagine going to klaipėda for the beaches
????????????????????

8 replies

why else?
horrible city

7 replies

Palanga > Klaipeda idiota

6 replies

no lol
kaunas > klaipeda > palanga > vilnius

5 replies

Kaunas??????????? Who goes to Kaunas for Beaches????

4 replies

not talking about beaches
just in general

3 replies

in general? Vilnius > Palanga > Kaunas > Klaipeda

2 replies

lol no

1 reply

lol yes

You can run c++ modules in python for stuff that has to be really fast.

1 reply

don't tell them

Run python on python using PyPy - that's fast bruh

Imagine not using Java (but I like c++ as well)

you need speed? go deeper, use assembler then

sorry bro going to start learn py this summer so can't help u rn:(

I'm sorry but currently I'm learning java, will answer your question in a few months when I start python :D

i can help.
but have u tried answering it yourself? have u written a code?

40 replies

yes I have written a code that can find the answer to any equation I think. I just have to derive the equation by hand. And I got the answer 3. Using the newtons method.
But I dont know how to do it with the halving method.
But I have no idea how to do this:
Equation set: f(x) = x2 + x-3 and g(x) = 2x2 - 2x-3
find the answer to that equation set when: g(x) = f(x)
could u help me with that last one there?

39 replies

you mean bisection method? i think its the easiest one out there, how come you're not understand
let me see your newton method first, copy it in ideone.com/ and write the link here

20 replies

thats java.. I am doing python

19 replies

you can change the language in bottom of the page, a dropdown menu. choose python or python 3

18 replies

ideone.com/ZU9MIo
I got the newton = 3
Is that correct?
But I also need the time newtons method took to find it, So what number show be in an = ?
I wrote an = 1.. but I dont think thats right
I think I need to print another one with print ("Newtons time", tid, "seconds")
or something

17 replies

cmon bruh, equation x-3=0 should have 3 as solution
what is function fder do? edit:np i just understood

15 replies

so was it right?

14 replies

sry, i was doing errand
shouldn't you check whether f(a) equal zero or not first?

13 replies

or is it included in tolerance check?
but why using loop inside function? shouldn't you use iterative function?
wait, is this math class or programming class?
if its math class should be okay

12 replies

programming class

11 replies

if i do my function like this, will you understand?
ideone.com/Y67E09
with maxiter and tolerance are global variable, defined outside of newton function
so instead using loop, you call the function itself inside the newton function

10 replies

I havent learned about maxiter and iter, so idk tbh

9 replies

maxiter is N in your code, iter is number of iteration done
sry, iedited my reply, maybe you havent read my explanation

8 replies

is it better learn the math first before writing the code?
have you learn to write fibbonaci or factorial function before?

7 replies

His loop version is better, you should always try to replace recurrence with looping unless it's very trivial and deterministic. With recurrence and big maxiter you may build call stack to dangerous size. In some languages you can hit stack limit pretty fast. It's neither optimal nor safe.

5 replies

make sense. thank you. you are right
i just cant imagine using numerical methods as loop exercise for programming class, because the way it is taught in math. so i made assumption(my bad) this should be done using recurrence.
and i actually only code briefly in my numerical analysis class, in which the problems barely hit any limit

4 replies

Yea, exactly. The recurrence solution is much closer to math theory behind it. That's why it's often being taught that way. I think (but I'm not sure because I'm still sleeping on functional languages) that languages like Erlang support recurrence in slightly different way underneath and recurrence may be preferred solution over there.

2 replies

in functional languages like Erlang or Haskell (and I would guess modern imperative like Rust and such) the compiler optimises recursion using a technique called tail-call optimisation to prevent overflowing the call stack. Or just converts it to a loop.

1 reply

Cool, thanks for confirmation.

btw. I checked stack call size limits and in Python it's 1000 and in Java 7-10k calls by default.

you need to write a code that first determines what kind of equation has been input and then solve it that way. so an equation that has a first term ax^2 is different than an equation which has first term ax^3 where a is any integer. you need to have if conditions in your code regarding that.
i can pm you my discord if u want more help.

17 replies

but I think I managed to do it using both methods now.
The answer is 3 tho right?? Cuz I got 3 using both methods. Or 2,9999998 using the halving method but ye

2 replies

are u trolling? because its obvious the answer is 3, even my 10 year old bro would know that

1 reply

xdddd ye I know but what values should I use for a and b in the halving method? does it even matter?

so this is good?
ideone.com/yTaKMq
ideone.com/ZU9MIo

13 replies

maybe use b=4?
are you in loop module right now?
thus should be good enough

12 replies

ok ty bro

but what about this btw:
I have to find the answer using any method I want:
f(x) = x2 + x - 3 and g(x) = 2x2 - 2x - 3
Find the answer to the equation set above when: g(x) = f(x)
can u do this one so i can see?

10 replies

make another function h(x)=g(x)-f(x) and find x that satisfy h(x)=0
like when you do f(x)=x-3=0
now do h(x)=0

9 replies

differential equation. we assume f0 = 0
f' (x) = 5 + f(x)
what is the answer to the one above. I got to:
0. 0.526...... 1.107...... 2.460........ 3.245......... 4.113............
5.072......... 6.132...... 7.303....
correct?

6 replies

how?? if you do the math manually you will find the solution either 0 or 3
what method do you use, newton or halving?

5 replies

Its a new task btw... its not this one: f(x) = x2 + x - 3 and g(x) = 2x2 - 2x - 3
I am talking about this task: differential equation. we assume f0 = 0
f' (x) = 5 + f(x)

3 replies

and what is this question asked? f(1)?

2 replies

I wrote the task here:
ideone.com/m9xqb6
can u do that for me please?

1 reply

s

s

x - 3 = 0
answer is -x3 = JW washed up

For your equation set f(x) = x2 + x-3 and g(x) = 2x2 - 2x-3:
def fSolve (x): #solve equation f(x) where you decide on x
fx = x*2 + x-3
gSolve
return fx
def gSolve(fx): #solve equation using f(x)
gx = 2*fx*2 - 2*fx-3
return gx
#print stuff if u wish
print(f'g(1) = {gSolve(fSolve(1))}') #g(1) = -3
print(f'g(3) = {gSolve(fSolve(-3))}') #g(3) = -27
print(f'g(-1) = {gSolve(fSolve(-1))}') #g(-1) = -15
print(f'g(3) = {gSolve(fSolve(-3))}') #g(3) = -27
not the prettiest thing i wrote and if i understand correctly what u mean by halving method (10*15=5*30), that only works for multiplication

2 replies

can u write it in ideone.com ?
U can also do it with the newton method or other methods u choose.
but yeah
Task:
f(x) = x^2 + x - 3 and g(x) = 2x^2 - 2x - 3
g(x) = f(x)

1 reply

You don't need to be an expert to know that
x - 3 = 0
means it's
3 - 3 = 0
so
0 = 0
Bye

1 reply

You expert!

There are many different pythons, but I think they are not poisoned at all? What you need for them, you wanna buy?

stop being lazy. you'll get nowhere in life asking for people to do YOUR stuff

2 replies

but I want examples... So I can use it later... How can I learn this without help..

1 reply

fuck, I came here assuming you would need help with your pet python

this isnt python overall, just math tho

1 reply

no

im a Python expert, but i dont see anything related to a Python here. The Pythonidae, commonly known as pythons, are a family of nonvenomous snakes found in Africa, Asia, and Australia. Among its members are some of the largest snakes in the world. WTF math has anything to do with it?

I literally just today had to code newtons method for solving the cubic root of a for a test in school
this is the code I came up with and it works so just put your own function in it and it should work
so lets say that the cubic root of a is x so that means that x*x*x=a
so the function is
x*x*x-a
and I then derivated it manually
a=input("the number from which you want the root from: ")
c=int(a)
x=1
d=10
g=0
#code runs until two numbers are closer than 0,000001 of each other
while (int(d)<<1):
q=(x-(x*x*x-c)/(3*(x*x)))
#code doesn't like decimals and I'm too retarded to sort it
d=(abs(q-x))*1000000
x=q
g=g+1
print("iterations: "+str(g))
print(q)
this only works for the cubic root of a number but you can put your own function in it if you know how

How about you simply google halving method and newtons method in Python, and apply whatever you find to your problem? It's better idea than asking people on HLTV

import math
def halving_method(a, b, f, tolerance):
c = (a + b) / 2
f_c = f(c)
if abs(f_c) < tolerance:
return c
else:
if math.copysign(1, f_c) == math.copysign(1, f(a)):
return halving_method(c, b, f, tolerance)
else:
return halving_method(a, c, f, tolerance)
a = -10
b = 10
tolerance = 0.1
f = lambda x: x - 3
print(f"The root of the function f is {halving_method(a, b, f, tolerance)}")

2 replies

the answe comes up as none? but the answer should be 3 tho?

Use stack overflow not the HLTV forum....

Imagine not using matlab to solve math equations

pastebin.ubuntu.com/p/q4g5wnqPZ6/
Here is the code i wrote when i studied this. You need to input the interval [a,b] where the solution is and the error epsilon. I use the array to calculate the error.

3 - 3 = 0
free
x = 3

just google on stackoverflow:
stackoverflow.com/questions/47659731/new..
stackoverflow.com/questions/14392208/how..

easy
import math
def solve(a , b , c):
d = math.sqrt(b**2 - (4 * a * c))
x1 = (-b - d) / (2 * a)
x2 = (-b + d) / (2 * a)
return x1,x2
k = int(input("You Choose equation type press 1 or 2 \n 1-) x - a = 0 \n 2-) ax^2 + bx + c = 0"))
if k == 1:
a = int(input("enter the number a"))
print("x1 = %s" %(-a))
elif k==2:
a = int(input("enter the a : "))
b = int(input("enter the b : "))
c = int(input("enter the c : "))
x1 , x2 = solve(a , b , c)
print("x1 ->" , x1)
print("x2 ->" , x2)
else:
print("enter 1 or 2")

Stackoverflow mate, its your best option :)

you can use something like docs.sympy.org/latest/modules/solvers/so..

2 replies

this is the way to go if learning/practicing isn't your purpose and you're only interesting and use it as a tool.

1 reply

cobra mens))))))))))))))

Sorry dude, I'm not doing your homework. If you are unable to figure that out on your own, I dont think ICT is for you

Nice try. Do your homeworks properly. The logic of "bisection" is quite easy to implement in Python (or in any language), but a bit too much to be placed here just in few code lines. It is more than two-three code lines if you do the code from scratch instead of using pre-existing libraries. And how to use a pre-existing library solution is not probably what your teacher is asking for.

this is not really a python question ^^

X = 3
???

I can help but I guess the problem is already solved.
Don't use python. Almost everything has a built in library and you don't actually get to know the concepts in depth

1 reply

I improved your code and I made it more transparent what's actually happening. I also added another example of a function as f(x) = x - 3 is resolvable by newton method in 1 iteration. Enjoy.
ideone.com/Iwofkg

I think you firstly need to add math library,
import matplotlib.pyplot as pl (I guess this is the right one)
just type some equation or give x some numbers and it automatically solves it unless it is topology