Math
1 Curves for exponential function versus logarithm (EM13MAT401, EM13MAT402)
library(plotly)
<- seq(1, 10, length.out = 10)
x_values <- exp(x_values) # Exponential function
y_exp <- log(x_values) # Logarithmic function
y_log
<- plot_ly(x = x_values, y = y_exp, type = 'scatter', mode = 'markers+lines', name = 'Exponential') %>%
exp_plot layout(title = "Exponential Function", xaxis = list(title = 'X'), yaxis = list(title = 'Y Exp'))
<- plot_ly(x = x_values, y = y_log, type = 'scatter', mode = 'markers+lines', name = 'Logarithmic') %>%
log_plot layout(title = "Logarithmic Function", xaxis = list(title = 'X'), yaxis = list(title = 'Y Log'))
# Combine the graphs
subplot(exp_plot, log_plot, nrows = 1) %>%
layout(title = "Exponential vs Logarithmic", clickmode = 'event+select')
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
# Change the colors of the graphs:
<- plot_ly(x = x_values, y = y_exp, type = 'scatter', mode = 'markers+lines', name = 'Exponential', marker = list(color = 'blue'), line = list(color = 'blue')) %>% layout(title = "Exponential Function", xaxis = list(title = 'X'), yaxis = list(title = 'Y Exp')) exp_plot
2 1st, 2nd, and 3rd degree functions. degree (EM13MAT301, EM13MAT302)
library(plotly)
# Function to generate 1st, 2nd and 3rd degree polynomials
<- function(degree) {
generate_data <- seq(-5, 5, length.out = 100)
x <- if (degree == 1) 2 * x + 1 else if (degree == 2) x^2 - 3 * x + 2 else x^3 - 2 * x^2 + x - 1
y list(x = x, y = y)
}
plot_ly() %>%
add_trace(x = generate_data(1)$x, y = generate_data(1)$y, mode = 'lines') %>%
layout(
title = '1st to 3rd Degree Polynomial',
sliders = list(list(
steps = lapply(1:3, function(degree) {
list(label = degree, method = "restyle",
args = list(list(y = list(generate_data(degree)$y))))
})
)) )
Suggestions:
Try modifying the graph, using/replacing alternatively, the commands below in the code snippet:
# Add colors and labels to improve visualization
plot_ly() %>% add_trace(x = generate_data(1)$x, y = generate_data(1)$y, mode = 'lines', name = '1º Grau', line = list(color = 'blue')) %>% add_trace(x = generate_data(2)$x, y = generate_data(2)$y, mode = 'lines', name = '2º Grau', line = list(color = 'green')) %>% add_trace(x = generate_data(3)$x, y = generate_data(3)$y, mode = 'lines', name = '3º Grau', line = list(color = 'red')) %>%
3 Sine and Cosine (EM13MAT409, EM13CIF301)
library(plotly)
# Values of x
<- seq(0, 2 * pi, length.out = 100)
x
plot_ly() %>%
add_trace(x = x, y = sin(x), mode = 'lines', name = 'Sine') %>%
add_trace(x = x, y = cos(x), mode = 'lines', name = 'Cosine') %>%
layout(
title = 'Comparison: Sine and Cosine',
xaxis = list(title = 'Angle (radians)'),
yaxis = list(title = 'Value'),
showlegend = TRUE
)
Suggestions:
Try modifying the graph, using/replacing the commands below in the code snippet:
# Increase the values of the sine and cosine functions
<- 2 y_sin <- amplification * sin(x) y_cos <- amplification * cos(x) amplification
4 Central tendency: mean and median (EM13MAT306, EM13MAT305)
library(plotly)
# Sample Data
set.seed(123)
<- rnorm(100, mean = 50, sd = 10)
data
# Statistics
<- mean(data)
media_val <- median(data)
median_val
plot_ly() %>%
add_trace(x = data, type = 'histogram', name = 'Distribution', opacity = 0.6) %>%
add_trace(x = c(media_val, media_val), y = c(0, 15), mode = 'lines', name = 'Mean', line = list(color = 'red')) %>%
add_trace(x = c(median_val, median_val), y = c(0, 15), mode = 'lines', name = 'Median', line = list(color = 'blue')) %>%
layout(title = 'Central Tendency',
xaxis = list(title = 'Values'),
yaxis = list(title = 'Frequency'),
showlegend = TRUE)
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
# Example data (increasing the number of data)
set.seed(123)
<- rnorm(1000, mean = 50, sd = 10) # Increased from 100 to 1000 data
5 Comparison of surface area and volume of a sphere (EM13MT05)
library(plotly)
plot_ly() %>%
add_trace(x = 1:10, y = 4 * pi * (1:10)^2, mode = 'lines+markers', name = 'Surface') %>%
add_trace(x = 1:10, y = (4/3) * pi * (1:10)^3, mode = 'lines+markers', name = 'Volume') %>%
layout(
title = 'Surface and Volume of a Sphere',
xaxis = list(title = 'Radius'),
yaxis = list(title = 'Value')
)
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
= 1:100
x = 1:100 # increases the no. of points in 10 times y
6 Sine function and parameter adjustment (EM13MAT403, EM13MAT406, EM13MAT408)
# Install the necessary packages
# install.packages("plotly")
library(plotly)
# Generating data
<- seq(0, 2 * pi, length.out = 500)
x
# Function to generate y based on parameter a
<- function(a, x) {
func return(a * sin(x))
# Values of parameter 'a' that will vary
<- seq(0.5, 3, length.out = 30)
a_values
# Creating a data frame with all values of a and x to plot
<- data.frame(
data x = rep(x, times = length(a_values)),
y = unlist(lapply(a_values, function(a) func(a, x))),
a = rep(a_values, each = length(x))
)
# Creating the interactive graph with animation
plot_ly(data,
x = ~x,
y = ~y,
frame = ~a,
type = 'scatter',
mode = 'lines') %>%
layout(
title = "Variation of the function y = a * sin(x) with different values of a",
xaxis = list(title = "x"),
yaxis = list(title = "y", range = c(-3, 3))
)
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
# Data generation
<- seq(0, 2 * pi, length.out = 1000) # Increased from 500 to 1000 for greater smoothness" x
7 Linear Regression (EM13MT10)
library(plotly)
<- function(ruido_func) {
dados set.seed(123)
<- seq(0, 5, length.out = 50)
x <- 2 * x + 5 + rnorm(50, 0, ruido_func)
desvio <- predict(lm(desvio ~ x), data.frame(x = x))
y_esperado list(x = x, desvio = desvio, y_esperado = y_esperado)
}
plot_ly() %>%
add_trace(x = dados(1)$x, y = dados(1)$desvio, mode = 'markers', type = "scatter", name = "Dados") %>%
add_trace(x = dados(1)$x, y = dados(1)$y_esperado, mode = 'lines', type = "scatter", name = "Linha de Regressão") %>%
layout(
title = 'Regressão Linear',
xaxis = list(title = 'X'), yaxis = list(title = 'Y'),
sliders = list(list(
currentvalue = list(prefix = "Desvio: "),
steps = lapply(seq(0, 5, 1), function(sd_value) {
list(label = as.character(sd_value),
args = list(list(y = list(dados(sd_value)$desvio, dados(sd_value)$y_esperado))))
})
)) )
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
= 50 # increase the no. of points to better visualize the scattering
length.out
lapply(seq(0, 50, 1) # increases the number of standard deviations, to visualize the value of its influence on the scattering of the points (note that above 3 standard deviations the scattering becomes very large, with little change).
lapply(seq(0, 3, 0.1) # changes the slider, to visualize the scattering more smoothly up to the value of 3 standard deviations