<jupyter_start><jupyter_text>Exercício da aula 12: 13.21. Otimização  Determinar o tempo e o valor da maior altitude. Fazer as letras a) e c) utilizando epsilon s = 10^-4 %<jupyter_code>import math
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline
#the function must necessarily take only one argument
def evaluate(range_for_evaluation, function):
    ordinate = []
    for i in range_for_evaluation:
        ordinate.append(function(i))
    return ordinate
# FOR ZOOMING IN in the graph: plt.axis([xmin,xmax,ymin,ymax]) ----------- (zoom range)
def plot(x,y, title, xlabel, ylabel, zoom, zoom_range):
    a = 0
    plt.figure(figsize=(13,13)) #(I need to find a nice size)
    plt.plot(x,y, color='magenta')
    if zoom == True:
        plt.axis(zoom_range)
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.axhline(y=0, color='k') #this makes the x axis appear
    plt.grid()
    plt.show()
    return a
def z(t):
    return 100 + (80/15)*(55 + (80*9.81/15))*(1-(math.e**-(t*(15/80)))) - (80*9.81/15)*t
xrange = [0.00001*i for i in range(-75000,1000000)]
zs = evaluate(xrange, z)
zplot = plot(xrange, zs, "Altitude z(t)", "Tempo t(s)", "z(m)", False, [0,0,0,0])
zplot = plot(xrange, zs, "Altitude z(t)", "Tempo t(s)", "z(m)", True, [3,4.5,160,200])<jupyter_output><empty_output><jupyter_text>Sendo assim, pelo método gráfico, o ponto ótimo se daria em t = 3.8 s, com o valor de aproximadamente 193 metros.<jupyter_code>def function(x):
    return 100 + (80/15)*(55 + (80*9.81/15))*(1-(math.e**-(x*(15/80)))) - (80*9.81/15)*x
x_lower = 0
x_upper = 10
i = 0
error = 2

while error > 10**(-4):
    
    i+=1
    d = 0.6180339887498949*(x_upper - x_lower)
    r = 1 - 0.5*(math.sqrt(5)-1)

    x1 = x_lower + d
    x2 = x_upper - d

    fx1 = function(x1)
    fx2 = function(x2)
    
    #print("i = ", i, " x1 = ", x1, " x2 = ", x2, " fx1 = ", fx1, " fx2 = ", fx2)

    if fx1 > fx2:
        #the OPTIMA lie in the lower subinterval
        x_lower = x2
        d2 = 0.6180339887498949*(x_upper - x_lower)
        x1 = x_lower + d2 
        x_opt = x1
        
    if fx1 < fx2:
        #the OPTIMA lie in the upper subinterval
        x_upper = x1
        d1 = 0.6180339887498949*(x_upper - x_lower)
        x2 = x_upper - d1
        x_opt = x2
        
    error = 100 * math.fabs((1 - r) * ((x_upper - x_lower)/(x_opt)))
print(i)
print(x1)
print(function(x1))
print(error)<jupyter_output>8.669537079438047e-05
StarcoderdataJupyterStructuredCleanDedup