Mathematica Asked on November 28, 2021
I have an assignment to use Module
and in which I have to implement:
That’s how the plot supposed to look like. Unfortunately, mine look like this:
So, what do you think is wrong?
Problem 1: The source of the error is the second ListLinePlot
. You can see this because if you click on the orange bubble {...} on the error and select Show Stack Trace
you get this:
> Message[ListLinePlot::lpn, 3.6]
> ListLinePlot[18/5, PlotStyle -> RGBColor[0, 1, 0]]
You are trying to plot a single number 18/5, the mean. You need to provide at least two points to get a line in ListLinePlot
. To fix this use ListLinePlot[{{0,mean},{Length[ang],mean}}]
.
Problem 2: You need to use AllTrue
instead of All
when checking the list of strings.
Also you can shorten your If
statements. If they fail and the Print
's are triggered, then integer
and string
get assigned the return value of Print
which is Null
. I assume you really want False
.
Problem 3: You want to pass in strings into your function {"i", "am", "so", "confused"}
, not the list of symbols {i,am,so,confused}
Putting it all together:
fungsi[ang_, str_] := Module[{integer, string, max, mean, gambar},
integer = AllTrue[ang, IntegerQ];
string = AllTrue[str, StringQ];
If[Not[integer], Print["not integers"]];
If[Not[string], Print["not strings"]];
max = Max[ang];
mean = Mean[ang];
gambar =
Show[
ListLinePlot[ang, PlotStyle -> Blue],
ListLinePlot[{{0,mean},{Length[ang],mean}}, PlotStyle -> {Green,Dashed}],
ListLinePlot[{{0,max}, {Length[ang],max}}, PlotStyle -> Red]
]
]
fungsi[{1, 2, 3, 4, 8}, {"i", "am", "so", "confused"}]
You can also add lines and other graphics to plots using Epilog
like this:
ListLinePlot[{1, 2, 3, 4},
Epilog -> {Purple, Dashed, Line[{{0, 2}, {4, 2}}]}]
Answered by flinty on November 28, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP