I have plotted this function and found the maximum, but I only know how to find the maximum in y, I need of corresponding value in x, but I do not know how to do it.
I tried it in different ways, but i'm not a programmer, is an exercise for the university.
I built a vector in which the maxima were contained
N = seq(0, 100, by=0.001)
VettoreMassimo <- vector(mode = "numeric")
for(j in N)
{
VettoreMassimo[j]=abs(1/(cos(2*pi*j*H*(1-1i*D)/Vs)))
}
m = max(VettoreMassimo)
and if I try to use the command which.max(VettoreMassimo) i find the index of the vector.
Can anybody help me?
Sarei molto grato
(I apologize for my bad english)
Hi @GiacomoLam try subsetting your vector N: N[which.max(VettoreMassimo)]. You may also want to check out the function optim. To see the documentation you can use help(optim).
Now that you've solved your problem, will you please mark this thread as solved? If you don't know how, here's how to do it?
However, please note that if this is your actual code, you once again omitted the * in these two lines:
and
Also, let me give you a better way for this problem. You actually don't need the for loop here:
# Funzione di amplificazione A2
H <- 10 # [m] Spessore strato
Vs <- 200 # [m/s] Velocità onde S
D <- 0.05 # [%] Smorzamento
# Funzione di amplificazione A2
A2 <- function(f) abs(x = (1/(cos(2 * pi *f * H * (1 - 1i * D) / Vs))))
# Calcolo massimo
N <- seq(from = 0, to = 100, by = 0.001)
VettoreMassimo <- A2(f = N)
m <- max(VettoreMassimo)
f0 <- N[which.max(x = VettoreMassimo)]
# Stampa risultati
cat("Valore massimo di A2:", m)
#> Valore massimo di A2: 12.73528
cat("Frequenza fondamentale f0:", f0)
#> Frequenza fondamentale f0: 4.987
Thank you all for your answers and help!
Thanks I'll try your way, it seems more efficient.
In the code I assure you that the * sign is there, but I do not know why when I copy the code it is not reported too ..