Exercício 2 Prova 1

Joao_Bianco · updated July 30, 2021 · R
# Gráfico em 3 dimensões

# Curva
# Superfície z=f(x,y)

x <- seq(-2,2, by=0.3)
y <- seq(-2,2,by=0.3)
mf<-function(s,r){
    z=s+r*1i; Mod(4*r^6+23*r^5+54*r^4+65*r^3+40*r^2+9*r-2)^2# f(x,y); auxiliar para gráfico
}

require(grDevices) # for trans3d
z <- outer(x, y, mf)
z[is.na(z)] <- 4
op <- par(bg = "white")
#persp(x, y, z, theta = 90, phi = 40, expand = 1)
#contour(x,y,z,levels=c(0.5,1,2))

#persp(x, y, z, theta = 30, phi = 40, expand = 1)->res

# Método de Euler para resolver a equação

gradmf<-function(u){ # gradiente de f(x,y)
    s=u[1]
    r=u[2]
    h=10^(-5)
    dnfx= mf(s+h,r)-mf(s-h,r)
    dnfy=mf(s,r+h)-mf(s,r-h)
    c(dnfx,dnfy)/(2*h)
} 

t0=0  # tempo inicial
tf=2  # t final 
e0=c(0,0) # condição inicial 
#mf(e0[1],e0[2]) # teste da escolha
n=1000
h=(tf-t0)/n  # Tamanho do passo


tt=seq(t0,tf,by=h)

Y=matrix(0,2,length(tt))
Y[,1]=e0

for ( i in 1:(length(tt)-1)){
    Y[,i+1]=Y[,i]-h*gradmf(Y[,i])
}


print("Taxa de juros mensal aproximada: ");Y[2,length(tt)]

#print("Ponto de minimo de f(z) ou raiz de p(z)"); Y[,length(tt)]
#print("Valor minimo aproximada"); mf(Y[1,length(tt)],Y[2,length(tt)])

#lines(trans3d(Y[1,],Y[2,],mf(Y[1,],Y[2,]), res), col = "blue", lwd = 2) # Inclui curvas

#plot(Y[1,],Y[2,],'l',col="blue")


#plot(tt,mf(Y[1,],Y[2,]),'l', col = "blue")

Comments

Please sign up or log in to contribute to the discussion.