How to find x and y of a point of a mathematical function in Rstudio ..?

hello everyone!
I have a function in R

A2 <- function(f) abs(1/(cos(2*pi*f*H*(1-1i*D)/Vs)))

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)
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).

thank you all!!
But I solved this way:


#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(1/(cos(2pifH(1-1i*D)/Vs)))


curve(A2, 0, 100, n = 100000, xlab="f [Hz]", ylab= "A2(f)", col="blue", main="Funzione di amplificazione A2", sub="Smorzamento D = 5 %", lwd="1")

Calcolo massimo

passoFrequenza = 0.001
FrequenzaMax = 100
N = FrequenzaMax/passoFrequenza
VettoreMassimo <- vector(mode = "numeric")
VetFreqA2 <- vector(mode = "numeric")
for(k in 1:N)
m = max(VettoreMassimo)
#Calcolo frequenza fondamentale
ind = which.max(VettoreMassimo)
f0 = VetFreqA2[ind]
#Stampa risultati
print("Valore massimo di A2")
print("Frequenza fondamentale f0")

I know it's not the most elegant way to do it, but it works!

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:


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

Created on 2019-02-23 by the reprex package (v0.2.1)

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 ..

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.