[informatica] pythoncode

Moderators: ArcherBarry, Fuzzwood

Reageer
Gebruikersavatar
Berichten: 4.502

[informatica] pythoncode

ik heb geen ervaring met python
ik heb deze code overgenomen van een YouTube video (toepassing van Regula Falsi)
Hoe kan ik voorkomen dat de tekst in de laatste regel op het scherm verschijnt in het geval er geen- of meerdere nulpunten zijn binnen het opgegeven interval.
python code.png

Gebruikersavatar
Moderator
Berichten: 9.781

Re: [informatica] pythoncode

Plaats het even als code "</>" - knop.

Dan hoef ik het niet over te tikken.

Gebruikersavatar
Berichten: 4.502

Re: [informatica] pythoncode

geen idee wat je hiermee bedoelt..

Gebruikersavatar
Moderator
Berichten: 9.781

Re: [informatica] pythoncode

Volledige bewerker & voorbeeld, paste de code, selecteer alles (of tenminste alle code, als je er ook iets bij schrijft), en klik op "</>" - knop.

Gebruikersavatar
Berichten: 4.502

Re: [informatica] pythoncode

regula falsi 1.rar
(438 Bytes) 91 keer gedownload

Gebruikersavatar
Moderator
Berichten: 9.781

Re: [informatica] pythoncode

Zo, bijvoorbeeld:

Code: Selecteer alles

from math import sin
def rfalsi(f,x1,x2,tol=1.0e-6,maxfpos=100):
    xh=0
    fpos=0
    ok=False
    if f(x1)*f(x2)<0:
        ok=True
        for fpos in range(1,maxfpos+1):
            xh=x2-(x2-x1)/(f(x2)-f(x1))*f(x2)
            if abs(f(xh))<tol: break
            elif f(x1)*f(xh)<0:
                 x2=xh
            else:
                 x1=xh
    else:
          print('Er is geen nulpunt ofwel er zijn meerdere nulpunten binnen het gegeven interval.')
    return xh,fpos, ok
y=lambda x: x**2-sin(x)**2-4*x+1
x1=float(input('Enter x1: '))
x2=float(input('Enter x2: '))
r,n,ok=rfalsi(y,x1,x2)
if ok:
    print('het nulpunt = %f na %d iteraties.'%(r,n))

Gebruikersavatar
Berichten: 4.502

Re: [informatica] pythoncode

Het werkt! :D
Bedankt..

Gebruikersavatar
Moderator
Berichten: 9.781

Re: [informatica] pythoncode

Graag gedaan. En kijk nog eens hoe je code kunt plaatsen, voor een eventueel volgende keer.
Is heel makkelijk, en ook makkelijker om binnen te halen. Zo' n rar moet ik eerst opslaan en uitpakken.

Gebruikersavatar
Berichten: 4.502

Re: [informatica] pythoncode

Zeker handig!

Code: Selecteer alles

# Input Section
from math import sin
import math
def f(x):
    return math.exp(x)+sin(x)

# Implementing False Position Method
def falsePosition(x0,x1,e):
    step = 1
    print('\n\n*** Regula Falsi methode implementatie ***')
    condition = True
    while condition:
        x2 = x0 - (x1-x0) * f(x0)/( f(x1) - f(x0) )
        print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))

        if f(x0) * f(x2) < 0:
            x1 = x2
        else:
            x0 = x2

        step = step + 1
        condition = abs(f(x2)) > e

    print('\nRequired root is: %0.8f' % x2)


# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Toegestane Fout: ')

# Converting input to float
x0 = float(x0)
x1 = float(x1)
e = float(e)

#Note: You can combine above two section like this
# x0 = float(input('First Guess: '))
# x1 = float(input('Second Guess: '))
# e = float(input('Toegestane fout: '))


# Checking Correctness of initial guess values and false positioning
if f(x0) * f(x1) > 0.0:
    print('Given guess values do not bracket the root.')
    print('Try Again with different guess values.')
else:
    falsePosition(x0,x1,e)

Reageer