Physics
1 Electric field and distance (EM13MAT305, EM13CNT302)
library(plotly)
# Constant k0 (approximately 8.99 × 10^9 N m²/C²)
<- 8.99e9
k0
# Distance values (avoiding d = 0 to avoid dividing by zero)
<- seq(0.1, 10, length.out = 100)
d
# Function to calculate the electric field
<- function(Q, d) {
calcular_campo * Q / d^2
k0
}
# Initial values of Q
<- 1e-6 # Initial charge in Coulombs
Q_inicial
# Interactive graph with slider to change Q
plot_ly(x = ~d, y = ~calcular_campo(Q_inicial, d), type = 'scatter', mode = 'lines') %>%
layout(
title = 'Electric Field (E) as a Function of Distance (d)',
xaxis = list(title = 'Distance (m)'),
yaxis = list(title = 'Electric Field (N/C)', type = 'log'),
sliders = list(
list(
active = 0,
currentvalue = list(prefix = "Charge (Q): "),
steps = lapply(seq(1e-7, 1e-5, length.out = 10), function(Q_value) {
list(
label = sprintf("%.1e", Q_value),
method = "restyle",
args = list('y', list(calcular_campo(Q_value, d)))
)
})
)
) )
Suggestions:
Try modifying the graph, using/replacing the commands below in the code snippet:
= list(title = 'Distance (m)', range = c(0, 10)), # Define the range for the X axis
xaxis = list(title = 'Electric Field (N/C)', type = 'log', range = c(1e3, 1e12)), # Adjust the scale of the Y axis yaxis
2 Uniformly varied rectilinear motion - MRUV (EM13CNT203, EM13CNT204)
library(ggplot2)
library(gganimate)
# Função para calcular a posição em MRUV
<- function(s0, v0, a, t) {
calcular_mruv + v0 * t + 0.5 * a * t^2
s0
}
# Dados
<- 0
s0 <- 5
v0 <- seq(0, 10, length.out = 100)
t <- seq(0, 5, by = 1)
a_values
# Criando um dataframe com os dados
<- data.frame()
dados_mruv for (a in a_values) {
for (time in t) {
<- calcular_mruv(s0, v0, a, time)
pos <- rbind(dados_mruv, data.frame(Tempo = time, Posição = pos, Aceleração = a))
dados_mruv
}
}
# Gráfico animado para MRUV
ggplot(dados_mruv, aes(x = Tempo, y = Posição, group = Aceleração)) +
geom_line(aes(color = as.factor(Aceleração))) +
labs(title = 'MRUV: Posição em Função do Tempo',
subtitle = 'Aceleração = {frame_time} m/s²',
x = 'Tempo (s)', y = 'Posição (m)', color = 'Aceleração') +
transition_time(Aceleração) +
ease_aes('linear')
Suggestions:
Try modifying the graph, using/replacing the commands below in the code snippet:
# Inside the ggplot() function
scale_color_manual(values = c("0" = "blue", "1" = "red", "2" = "green", "3" = "purple", "4" = "orange", "5" = "brown")) + # Define specific colors for each acceleration
3 Gravitational force and distance (EM13CNT401, EM13CNT402)
# Função para calcular a força gravitacional
<- function(m1, m2, d) {
calcular_gravitacional <- 6.67e-11
G * m1 * m2 / d^2
G
}
# Dados
<- 5.97e24 # Massa da Terra em kg
m1 <- seq(1e22, 1e24, length.out = 5)
m2_values <- seq(1e6, 4e8, length.out = 100)
d
# Criando um dataframe com os dados
<- data.frame()
dados_gravitacional for (m2 in m2_values) {
for (distancia in d) {
<- calcular_gravitacional(m1, m2, distancia)
forca <- rbind(dados_gravitacional, data.frame(Distância = distancia, Força = forca, Massa = m2))
dados_gravitacional
}
}
# Gráfico animado para força gravitacional
ggplot(dados_gravitacional, aes(x = Distância, y = Força, group = Massa)) +
geom_line(aes(color = as.factor(Massa))) +
labs(title = 'Força Gravitacional em Função da Distância',
subtitle = 'Massa = {frame_time} kg',
x = 'Distância (m)', y = 'Força Gravitacional (N)', color = 'Massa') +
transition_time(Massa) +
ease_aes('linear')
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
# Inside the ggplot() function
theme_minimal() + # Simple theme
4 2nd. Newton’s Law - Mass and Acceleration (EM13CNT202, EM13CNT203)
# Função para calcular a força
<- function(m, a) {
calcular_forca * a
m
}
# Dados
<- seq(0, 10, length.out = 100)
a <- seq(1, 20, by = 5)
m_values
# Criando um dataframe com os dados
<- data.frame()
dados_newton for (m in m_values) {
for (aceleracao in a) {
<- calcular_forca(m, aceleracao)
forca <- rbind(dados_newton, data.frame(Aceleração = aceleracao, Força = forca, Massa = m))
dados_newton
}
}
# Gráfico animado para Segunda Lei de Newton
ggplot(dados_newton, aes(x = Aceleração, y = Força, group = Massa)) +
geom_line(aes(color = as.factor(Massa))) +
labs(title = 'Força (F) em Função da Aceleração (a)',
subtitle = 'Massa = {frame_time} kg',
x = 'Aceleração (m/s²)', y = 'Força (N)', color = 'Massa') +
transition_time(Massa) +
ease_aes('linear')
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
geom_line(aes(color = as.factor(Massa)), size = 1) + # Lines with default thickness
5 Ohm’s Law - resistance, potential, and current (EM13CNT303, EM13CNT304)
library(ggplot2)
library(gganimate)
# Dados
<- seq(1, 10, by = 1) # Resistência de 1 a 10 Ohms
resistencias <- seq(0, 5, length.out = 100) # Corrente de 0 a 5 Amperes
corrente <- data.frame()
dados
# Criando um dataframe com os dados
for (R in resistencias) {
for (I in corrente) {
<- I * R # Calculando a Tensão
V <- V * I # Calculando a Potência
P <- rbind(dados, data.frame(Corrente = I, Resistência = R, Tensão = V, Potência = P))
dados
}
}
# Gráfico animado para Lei de Ohm e Potência
<- ggplot(dados, aes(x = Corrente, y = Tensão)) +
p geom_line(aes(color = as.factor(Resistência), group = Resistência)) +
geom_point(aes(size = Potência), alpha = 0.5) +
labs(title = 'Lei de Ohm: Tensão vs Corrente',
subtitle = 'Resistência = {frame_time} Ohms',
x = 'Corrente (A)', y = 'Tensão (V)', color = 'Resistência', size = 'Potência (W)') +
transition_time(Resistência) +
ease_aes('linear')
# Exibindo o gráfico
p
Suggestions:
Try modifying the graph, using/replacing the commands below in the code snippet:
geom_point(aes(size = Power), alpha = 0.6) + # Points with size according to power
6 Electricity and residential consumption (EM13CNT405, EM13CNT306)
library(ggplot2)
library(gganimate)
# Dados dos eletrodomésticos (potência em kW)
<- data.frame(
eletrodomesticos Nome = c("Lâmpada", "Ventilador", "Geladeira", "Computador"),
Potencia = c(0.1, 0.15, 0.2, 0.5) # Potências em kW
)
# Tempo (em horas) para o qual calculamos o consumo
<- seq(0, 24, length.out = 100) # 0 a 24 horas
tempo
# Criando um dataframe com o consumo de energia
<- data.frame()
dados_consumo
for (i in 1:nrow(eletrodomesticos)) {
<- eletrodomesticos$Potencia[i] * tempo # Consumo em kWh
consumo <- rbind(dados_consumo,
dados_consumo data.frame(Tempo = tempo,
Consumo = consumo,
Eletrodomestico = eletrodomesticos$Nome[i]))
}
# Gráfico animado para o consumo de energia
<- ggplot(dados_consumo, aes(x = Tempo, y = Consumo, color = Eletrodomestico, group = Eletrodomestico)) +
p geom_line(size = 1.2) +
labs(title = 'Consumo de Energia Residencial (kWh)',
x = 'Tempo (horas)', y = 'Consumo (kWh)', color = 'Eletrodoméstico') +
transition_states(Eletrodomestico, transition_length = 2, state_length = 1) +
ease_aes('linear')
# Exibindo o gráfico
p
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
geom_line(size = 1.4) + # Lines with a little more thickness for better visibility
7 Temperature conversion - Celsius, Kelvin, and Fahrenheit (EM13MAT403, EM13CNT205)
library(ggplot2)
library(gganimate)
# Temperature range in Celsius
<- seq(-100, 100, by = 1)
celsius
# Calculating the conversions
<- data.frame(
temp_data Celsius = celsius,
Fahrenheit = celsius * 9/5 + 32,
Kelvin = celsius + 273.15
)
# Rearranging the data for better visualization
<- reshape2::melt(temp_data, id.vars = "Celsius")
temp_data_long
# Animated graph
<- ggplot(temp_data_long, aes(x = Celsius, y = value, color = variable)) +
p geom_line(size = 1.2) +
labs(title = 'Temperature Conversion',
x = 'Temperature to Celsius', y = 'Converted Temperature',
color = 'Scale') +
transition_reveal(Celsius) +
ease_aes('linear')
# Displaying the graph
p
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
ease_aes('cubic-in-out') + # Change in animation for smoother transition
8 Thermal expansion - superficial, volumetric, and liquids (EM13CNT304, EM13CNT305)
library(ggplot2)
library(patchwork)
# Valores iniciais
<- 100 # Área inicial (cm^2)
area_inicial <- 100 # Volume inicial (cm^3)
volume_inicial <- 1.2e-5 # Coeficiente de dilatação superficial
beta <- 3.6e-5 # Coeficiente de dilatação volumétrica
gama_solido <- 4.0e-5 # Coeficiente de dilatação volumétrica de líquidos
gama_liquido
# Variação de temperatura (0 a 100 °C)
<- seq(0, 100, by = 1)
delta_T
# Cálculo das dilatações
<- area_inicial * beta * delta_T
dilatacao_superficial <- volume_inicial * gama_solido * delta_T
dilatacao_volumetrica <- volume_inicial * gama_liquido * delta_T
dilatacao_liquido
# Dataframes para gráficos
<- data.frame(Temperatura = delta_T, Dilatacao = dilatacao_superficial)
dados_superficie <- data.frame(Temperatura = delta_T, Dilatacao = dilatacao_volumetrica)
dados_volumetrica <- data.frame(Temperatura = delta_T, Dilatacao = dilatacao_liquido)
dados_liquido
# Criando os gráficos
<- ggplot(dados_superficie, aes(x = Temperatura, y = Dilatacao)) +
grafico_superficie geom_line(color = "blue") +
labs(title = "Dilatação Superficial", x = "Temperatura (°C)", y = "ΔA (cm^2)")
<- ggplot(dados_volumetrica, aes(x = Temperatura, y = Dilatacao)) +
grafico_volumetrica geom_line(color = "red") +
labs(title = "Dilatação Volumétrica", x = "Temperatura (°C)", y = "ΔV (cm^3)")
<- ggplot(dados_liquido, aes(x = Temperatura, y = Dilatacao)) +
grafico_liquido geom_line(color = "green") +
labs(title = "Dilatação de Líquidos", x = "Temperatura (°C)", y = "ΔV (cm^3)")
# Exibindo os gráficos lado a lado
+ grafico_volumetrica + grafico_liquido grafico_superficie
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
# Below geom_line()
geom_point(color = "blue", size = 2) + # Adding points
9 Alternating Current - Capacitor (EM13CNT202)
library(plotly)
# Values of x
<- seq(0.5 * pi, length.out = 100)
x
plot_ly() %>%
add_trace(x = x, y = sin(x), mode = 'lines', name = 'Potential') %>%
add_trace(x = 1.4*x, y = 0.9*cos(x)-0.4, mode = 'lines', name = 'Current') %>%
layout(
title = 'Voltage and Current in a Capacitor',
xaxis = list(title = 'Angle (radians)', range=c(0.15)),
yaxis = list(title = 'Value'),
showlegend = TRUE
)
Suggestions:
Try modifying the graph, using/replacing the following commands in the code snippet:
add_trace(x = x, y = sin(x), mode = 'lines', name = 'Potencial',
line = list(color = 'blue', width = 2, dash = 'dot')) %>% # Blue dotted line
10 Heat engine efficiency as a function of temperature (EM13CNT102, EM13CNT103)
library(ggplot2)
library(plotly)
# Function to generate data and graph based on half-efficiency temperature (or saturation temperature)
<- function(b) {
generate_plot_data <- 1 # maximum efficiency of the heat engine
a <- seq(0, 300, length.out = 100) # temperature range
x <- a * x / (b + x) # equation that relates temperature to efficiency (maximum = 1)
y data.frame(x, y)
}
# Initialize the graph with b = 1
<- 1 # initial value of b (half-saturation temperature)
initial_b <- generate_plot_data(initial_b)
plot_data
# Creating the initial graph using the ggplot2 package
<- ggplot(plot_data, aes(x = x, y = y)) +
p geom_line() +
labs(title = paste("Efficiency of a heat engine as a function of temperature"),
x = "Temperature (C)",
y = "Fraction of Efficiency") +
theme_minimal() +
ylim(0, 1)
# Converting to a plotly object
<- ggplotly(p)
fig
# Defining the steps for the slider
<- lapply(seq(0, 100, by = 10), function(b) {
steps list(
label = as.character(b),
method = "restyle",
args = list(
list(
x = list(generate_plot_data(b)$x),
y = list(generate_plot_data(b)$y)
),list(title = paste("Efficiency as a function of half-saturation value:", b)) )
)
})
# Adding the slider for the 'b' parameter (half-saturation temperature)
<- fig %>%
fig layout(
sliders = list(
list(
active = 0,
currentvalue = list(prefix = "Half-Saturation: "),
steps = steps
)
)
)
# View the graph
fig
Suggestions:
Try modifying the graph, using/replacing alternatively the commands below in the code snippet:
# Inside steps
list(
x = list(generate_plot_data(b)$x),
y = list(generate_plot_data(b)$y),
line = list(color = colorRampPalette(c("blue", "green"))(11)[b/10 + 1]) # Color range varying from blue to green
),