- #1

Eclair_de_XII

- 1,083

- 91

Okay, so my algorithm looks something like this:

====

1. Locate mid-point of the interval . This is our estimate of the local max.

2. Evaluate .

3. Divide the main interval into two subintervals: a left and right of equal length.

4. Check to see if there is a point in the interval such that . If there is, check, because the maximum is there.

5. Do the same for .

6. If there is not a point in either interval such that greater than the current iteration of , then set . This is the maximum.

====

Edit: I saw what was wrong with my code. I forgot to put the () after the .any method.

It's odd, though, that whenever I use this function for this somewhat messy, amateur module:

https://github.com/gchang12/scientific_programming/blob/master/inheritance

with the parameters (x0=-pi/2,xf=pi/2,h=cos,N=50), the module I made implementing it returns no minimum points, while the module here returns a point exponentially close to zero (which I should maybe work on fixing).

====

1. Locate mid-point of the interval . This is our estimate of the local max.

2. Evaluate .

3. Divide the main interval into two subintervals: a left and right of equal length.

4. Check to see if there is a point in the interval such that . If there is, check, because the maximum is there.

5. Do the same for .

6. If there is not a point in either interval such that greater than the current iteration of , then set . This is the maximum.

====

Python:

```
from numpy import vectorize, linspace
def find_max(x0,xf,h,N=100):
xm=(xf+x0)/2; f=vectorize(h)
xleft=linspace(x0,xm,N); xright=linspace(xm,xf,N)
if (f(xleft)-f(xm)>1E-16).any:
return find_max(x0,xm,h,N)
if (f(xright)-f(xm)>1E-16).any:
return find_max(xm,xf,h,N)
else:
return xm
t=(-1,1,lambda x: x**2)
find_max(*t)
```

Edit: I saw what was wrong with my code. I forgot to put the () after the .any method.

Final version.:

```
from numpy import vectorize, linspace, pi,sin
def diff(f,x1,x2):
return f(x1)-f(x2)
def find_crt(x0,xf,h,N=100,local='max'):
local.lower()
assert local=='min' or local=='max'
xm=(xf+x0)/2; f=vectorize(h)
xleft=linspace(x0,xm,N); xright=linspace(xm,xf,N)
t1=[xleft,xm]; t2=[xright,xm]
if local=='min':
t1,t2=t1[::-1],t2[::-1]
if (diff(f,*t1)>=1E-16).any():
return find_crt(x0,xm,h,N)
if (diff(f,*t2)>=1E-16).any():
return find_crt(xm,xf,h,N)
else:
return xm
```

It's odd, though, that whenever I use this function for this somewhat messy, amateur module:

https://github.com/gchang12/scientific_programming/blob/master/inheritance

with the parameters (x0=-pi/2,xf=pi/2,h=cos,N=50), the module I made implementing it returns no minimum points, while the module here returns a point exponentially close to zero (which I should maybe work on fixing).

Last edited: