I am new at python. How can i translate this pseudocode to python? Thanks...
Screenshot_1.png

I tried and got a code like this but it doesn't work. where do you think the mistake is? Thanks..

import numpy as numpy

def modfalsepos(xl, xu, es, imax, xr, iter, ea):
  return (-0.6*(x**2))+(2.4*x)+(5.5)

xl=2
xu=6
es=0.001
imax=1000
iter=1
ea=0.0001

fl=f(fxl)
fu=f(xu)

while (True): 
 { 
  xrold == xr
  xr = xu - (fu*(xl-xu)/(fl-fu))
  fr = f(xr)
  iter = iter + 1

  if (xr != 0) :
    ea = abs((xr - xrold) / xr) * 100

  test = fl * fr
  if (test < 0) :
    xu = xr
    fu = f(xu)
    iu = 0
    il = il +1 

    if (il >= 2) :
      fl = fl / 2

  elif (test > 0) :
    xl = xr
    fl = f(xl)
    il = 0
    iu = iu + 1

    if (iu >= 2) :
      fu = fu / 2
  else :
    ea = 0

  if (ea < es or iter >= imax) :
    break
 }
  ModFalsePos = xr

There are a couple of problems.

The spec says to run for xl=2 xu=6 es=0.001 imax=1000 iter=1 ea=0.0001. The problem is that iter is not a parameter. Should this be xr=1?

You don't need to import numpy. Remove it.

You have to define the function f(x), likely as

def f(x): return -0.6 * x * x + 2.4 * x + 5.5

then indent the rest of your code under modfalsepos (define it with the same capitalization as in the spec).

Take out lines 6-11.

Take out the brace brackets. You are not writing c/c++ or java. This is Python and brace brackets are used for dicts and sets.

The line

xrold == xr

compares xrold and xr. It is not an assignment statement.

You may run into a problem with iu. If the code for elif test > 0.0 executes before the code for if test < 0.0 then iu will be undefined when you try to increment it.

In Python you don't assign a value to the function name at the end. You just do

return xr

Finally, you need a mainline to actually call your code. Add

if __name__ == '__main__':
    result = ModFalsePos(xl=2.0, xu=6.0, es=0.001, imax=1000, xr=1.0, ea=0.0001)       
    print(f'{result=}')

at the end

As I said, I am new to python and unfortunately I have been able to implement very little of what you said. Sorry. could you be more descriptive?
Thanks....

What parts are unclear? I think I've pretty much done everything but show you the complete correct code which is almost line for line what is given in the pseudo-code.

Actually, these are what I understand from what you say.

My code fails in the fl = f (xl) and fu = f (xu) parts. I still don't understand what these are and my mistakes.

Normally i work in c ++ and python is a completely different language.

def f(x):
  return -0.6 * x * x + 2.4 * x + 5.5

def modfalsepos(xl, xu, es, imax, xr, iter, ea):

fl=f(xl)
fu=f(xu)

while True: 

  xrold = xr
  xr=xu-fu*(xl-xu)/(fl-fu)
  fr = f(xr)
  iter = iter + 1

  if xr != 0 :
    ea = abs((xr - xrold) / xr) * 100

  test = fl * fr

  if test < 0.0 :
    xu = xr
    fu = f(xu)
    iu = 0
    il = il +1 

    if il >= 2 :
      fl = fl / 2

  elif (test > 0.0) :
    xl = xr
    fl = fxl
    il = 0
    iu = iu + 1

    if iu >= 2 :
      fu = fu / 2

  else :
    ea = 0

  if (ea < es or iter >= imax) :
    break

  return xr
  if ModFalsePos == xr:
    result = ModFalsePos(xl=2.0, xu=6.0, es=0.001, imax=1000, xr=1.0, ea=0.0001)       
    print(f'{result=}')

You're most of the way there. All the code under def ModFalsePos has to be indented to belong to that function, and you have to do a little initialization of some variables. That and fix a typo or two.

def f(x): 
    return -0.6 * x * x + 2.4 * x + 5.5

def ModFalsePos(xl, xu, es, imax, xr, ea):

    iter = 0
    iu   = 0.0
    fl   = f(xl)
    fu   = f(xu)

    while True:

        xrold = xr
        xr = xu - fu * (xl - xu) / (fl - fu)
        fr = f(xr)
        iter += 1

        if xr != 0.0 :
            ea = abs((xr - xrold) / xr) * 100

        test = fl * fr

        if test < 0.0:
            xu = xr
            fu = f(xu)
            iu = 0.0
            il += 1

            if il >= 2:
                fl = fl / 2.0

        elif test > 0.0:
            xl = xr
            fl = f(xl)
            il = 0
            iu += 1

            if iu >= 2:
                fu = fu / 2.0
        else:
            ea = 0.0

        if ea < es or iter > imax:
            break

    return xr

if __name__ == '__main__':
    result = ModFalsePos(xl=2.0, xu=6.0, es=0.001, imax=1000, xr=1.0, ea=0.0001)
    print(f'{result=}')

I am grateful to you. Thank you so much. Have a nice day..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.