Help with Riemann sum graph #392
-
I'm pretty new to Typst (been using for a few days) and really new to CeTZ (been trying it out tonight). I'm trying to plot a graph of f(x) = 16 - x^2 and its left-hand Riemann sum (from [0,6], 6 intervals). I feel like I'm close, but I can't quite work out how to fix this. #canvas(length: 0.4in, {
let f = x => 16 - x * x
let barColor = color.green
for i in (0, 1, 2, 3, 4, 5) {
let height = f(i)
if (height < 0) {
barColor = color.red
}
draw.fill(barColor.lighten(69%).darken(8%))
draw.rect(
(i, 1),
(i+1, height),
)
}
plot.plot(size: (8, 6),
x-grid: true,
y-grid: true,
x-tick-step: 1,
y-tick-step: 4,
{
plot.add(
domain: (0, 6),
x => f(x))
})
}) Below is the graph with the for loop commented out, and below that is what I want it to look like (done with Desmos) |
Beta Was this translation helpful? Give feedback.
Answered by
ThatOneCalculator
Dec 11, 2023
Replies: 1 comment 1 reply
-
I solved it! Now onto #393 #canvas(length: 0.4in, {
// Function
let f = x => 16 - x * x
// Plot
plot.plot(size: (6, 6),
x-grid: true,
y-grid: true,
x-tick-step: 1,
y-tick-step: 4,
{
plot.add(
domain: (0, 6),
x => f(x))
}
)
// Left-hand Riemann sum
let barColor = color.green
let fixedOffset = 3.34
for i in (0, 1, 2, 3, 4, 5) {
let height = f(i)
if (f(i) < 0) {
barColor = color.red
}
//draw.fill(barColor.lighten(70%).darken(8%))
draw.stroke(barColor.darken(30%))
draw.rect(
(i, fixedOffset),
(i + 1, (height / 6) + fixedOffset),
)
}
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Styled it a bit nicer