Comparazione numeri
#Consider the numbers 3.493427e+19 and 2.392993e+23. Then say whether both
numbers are small and the first is larger then the second.
3.493427*exp(19)
2.392993*exp(23)
#entrambi i numeri sono grandi, FALSE
Trovare i numeri riciclati
# Consider the expression abs(-1:2)+log(1:2). Which of the following stataments holds?
#(a) The first recycled number was 2
#(b) The first recycled number was 0
#(c) There was no need to recycle any argument.
#(d) The first recycled number was 1
abs(-1:2) #4 elementi
log(1:2) #2 elementi
abs(-1:2)+log(1:2)
log(1)+abs(1)
log(1)
#il primo numero reciclato è log(1)=0
Trovare il punto piu' basso del grafico in un intervallo
#Draw the line and the parabola y = −x+1, y = x^2 +x+2 over the interval [−2,3] and find
the ordinate of the lowest point belonging to the graphs.
linea <- function(x) -x+1
parabola <- function(x) x^2+x+2
curve(linea,-2,3) #gioco con ylim=c(,) per avere la panoramica completa della situazione
curve(parabola,add=T)
#capisco che è la linea ad avere l'ordinata più bassa
optimise(linea,c(-2,3))
#$objective -1.9999 da arrotondare a -2
SCORCIATOIA
#disegnate la retta y=x+1 e la parabola y=x^2-x-1 nell'intervallo [-2,2] e trovate l'ordinata
del punto più basso del grafico
retta <- function(x) x+1
parabola <- function(x) x^2-x-1
optimise(retta,c(-2,2)) #-0.99
optimise(parabola,c(-2,2)) #-1.25, che ovviamente scelgo come corretto
Considerazioni grafiche su punti random e linee e curve date
#Let x <- seq(0.4,3,len=11) and y <- 10+sin(10*x). Consider the functions g(x) = exp(x)
and h(x) = -2*x + 5. Plot both the functions on the interval 0 ≤ x ≤ 3 and add the points
defined by x and y. Which of the following sentence is true?
#(a) The curve is mostly above the line and 2 points are below the graph of the
exponential.
#(b) The curve is mostly above the line and 4 points are below the graph of the
exponential.
#(c) The curve is mostly above the line and 5 points are below the graph of the
exponential.
#(d) The curve is mostly above the line and 3 points are below the graph of the
exponential.
x <- seq(0.4,3,len=11)
y <- 10+sin(10*x)
f1 <- function(x) exp(x)
f2 <- function(x) -2*x +5
plot(x,y,xlim=c(0,3),ylim=c(-20,20)) #fondamentale il plot come primo grafico, nel quale di
deve giocare con xlim e ylim
curve(f1,col="pink",add=T) #aggiungo il colore al grafico della funzione esponenziale così
da riconoscerla
curve(f2,add=T)
Somma di elementi in vettori dagli elementi casuali
#Type set.seed(107) and press enter. Then immediately store in a vector 100 normal
pseudo-random numbers with mean 5 and standard deviation 2. Compute the sum of 8
components of the vector, starting from position 51.
set.seed(107)
vettore <- c(rnorm(100,5,2))
sum(vettore[51:58])
Somma di elementi di un vettore dato dal prodotto di una matrice
dai numeri random e un vettore dato
# Type set.seed(177) and press enter. Then immediately create a 4x3 matrix, filled by rows
with (standard) random uniform numbers. Let u be the product of the matrix times the
vector (−2,−1,0). Finally, provide the sum of the components of u.
set.seed(177)
A <- matrix(c(runif(12)),4,3,byrow=T)
b <- c(-2,-1,0)
u <- A%*%b #fondamentale il %*%
sum(u) Minimizzazione di una funzione in 1 variabile
#Minimize, if possible, the following function: f(x) = x^4 −x^3 −3*x^2 −4*x+4. Write the
objective function at the optimum or -999 if there is no solution.
#la funzione è minimizzabile perchè il termine di grado più grande è x^4 (non sarebbe
massimizzabile)
f <- function(x) x^4-x^3-3*x^2-4*x+4
curve(f,-100,100)
min <- optimise(f,c(-100,100))
points(min[1],min[2])
#$objective 8.285654
Minimizzazione di una funzione di 1 variabile in un intervallo
#Minimize, if possible, the following function over the interval [0,3]: f(x) = x^4 -3*x^3 -3*x^2
-2*x-4. Write the objective function at the optimum or -999 if there is no solution.
f <- function(x) x^4 -3*x^3 -3*x^2 -2*x-4
optimise(f,c(0,3))
Massimizzazione di una funzione di 1 variabile
#Maximize, if possible, the following function over the interval [−3,0]: f(x) = -x^4 -x^3 +4x^2
-x -1. Write the objective function at the optimum or -999 if there is no solution.
#è massimizzabile -> -x^4 (non minimizzabile)
f <- function(x) -x^4 -x^3 +4*x^2 -x -1
optimise(f,c(-3,0),maximum=T)
#$objective (valore d’ottimo)
Trovare la radice piu’ a destra di una funzione di 1 variabile
#Find the righmost root of the equation x^3 +4*x^2 +4*x +1 = 0
f <- function(x) x^3 +4*x^2 +4*x +1
curve(f,-1,0) #gioco con l’intervallo per isolare l’intersezione più a destra
abline(h=0)
uniroot(f,c(-0.6,0))
#$root Samsung & apple
#In the smartphone market, the number of customers (in billions) of Samsung and Apple
can be modelled by the functions f(x) = x/100+0.2exp(0.09*x), and g(x) = 0.33+0.025x,
where x > 0 denotes year 2000+x. When is Samsung reaching one billion customers for
the first time?
fsamsung <- function(x) x/100+0.2*exp(0.09*x)
fsamsungMODIFICATA <- function(x) fsamsung(x)-1 #non so ancora bene se il -1 sia
relativo al dato “one billion”
uniroot(fsamsungMODIFICATA, c(0,30))
Contare graficamente il numero di intersezioni(soluzioni) tra due
funzioni di 2 variabili
#How many solutions do you have in the system of non-linear equations: x^2 −2xy+y^2
+2x =0,(x+1)^2 + (y−1)^2 = 4?
e1 <- function(x,y) x^2-2*x*y+y^2+2*x
e2 <- function(x,y) (x+1)^2+(y-1)^2-4 #porto tutto a sinistra dell'uguale
x <- seq(-10,10,len=101)
y <- seq(-10,10,len=101)
z1 <- outer(x,y,e1)
z2 <- outer(x,y,e2)
contour(x,y,z1,levels=0)
contour(x,y,z2,levels=0,add=T)
#gioco con i numeriin seq per trovare il punto dell'immagine in cui i grafici si intersecano
#(x+1)^2+(y-1)^2-4 devo riconoscerla come circonferenza
Risolvere un sistema di + funzioni di 2 variabili
#Solve the non-linear system of equations 2*x + 5*y = exp(-x^2) + 7, 4*x-3*y = exp(-y^2) +
7, and write the x coordinate of the solution.
f1 <- function(x,y) 2*x + 5*y -exp(-x^2) -7 #portiamo tutto a SX
f2 <- function(x,y) 4*x-3*y -exp(-y^2) -7
x <- seq(0,10,len=101)
y <- seq(0,10,len=101)
z1 <- o
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.
Scarica il documento per vederlo tutto.