Ask Your Question
1

plot scattered data

asked 2023-01-07 13:58:18 +0100

moon gravatar image

updated 2023-01-07 14:04:46 +0100

I'm totally new and ingnorant in sage however trying to learn by doing. Thanks to other tips that I got here, I managed to make a real progress and now struggling with something that is pretty straightforward in other environments not in sage because of the logic (that I'm still missing) i'm putting the full example that I simplified to focus on the issue. i;e in practice I don't have these two functions. One has to understand that I have no contro on the structure of the data that I need to plot. it's simply two vectors u & v in R^2

#example:
f(x) = 2*sin(2*x+4)
g(x) = 3*sin(-x-4)
eq=f-g
pf = plot(f(x), (x,-pi,pi), color="red", fill=max_symbolic(f(x),g(x)), 
          fillcolor="lightgreen")
pg = plot(g(x), (x,-pi,pi), color="blue")
show(pf+pg)
nbi=20
endpoints = [-pi+2*pi*i/(nbi-1) for i in range(nbi)]
c=[]
for i in range(nbi-1):
    try:
        a=endpoints[i]
        b=endpoints[i+1]
        sol=eq.find_root(a,b)
        c.append(n(sol,digits=3))
    except RuntimeError:
        rep="no sol" #sometime I need to treat that properly
yc=[f(k) for k in c]
mypoints=[[c[k],f(c[k])] for k in range(len(c))]
print(mespoints)
# problems appear from here
ph=plot(mypoints, marker="*",color="black")
show(pf+pg+ph)

Basically I just need to approx highlight the zeros of f-g (intersection points) any help is a step further in my learning path. thx M

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-01-07 14:51:19 +0100

dan_fulea gravatar image

The following worked for me:

# example:
f(x) = 2*sin(2*x + 4)
g(x) = 3*sin(-x - 4)

eq = f - g
pf = plot(f(x), (x, -pi, pi)
          , color="red"
          , fill=max_symbolic(f(x),g(x))
          , fillcolor="lightgreen")
pg = plot(g(x), (x, -pi, pi)
          , color="blue")
# show(pf + pg)

nbi = 20
endpoints = [-pi + 2*pi*k/(nbi - 1) for k in range(nbi)]
c = []

for i in range(nbi-1):
    try:
        a = endpoints[i]
        b = endpoints[i + 1]
        sol = eq.find_root(a, b)
        c.append(n(sol, digits=3))
    except RuntimeError:
        rep = "no sol" #sometime I need to treat that properly

yc = [f(k) for k in c]
mes_points = point([ (p, f(p)) for p in c], marker='o', color='maroon', size=60)
print(mes_points)
ph = plot(mes_points)

show(pf + pg + ph)

Your points have to be constructed such that a graphics object results from this.

sage: type(mes_points)
<class 'sage.plot.graphics.Graphics'>

It should also work:

show(pf + pg + mes_points)

Also take a look at sage point doc, the section with

sage.plot.point.point2d(points, alpha=1, aspect_ratio='automatic', faceted=False, legend_color=None, legend_label=None, marker='o', markeredgecolor=None, rgbcolor=(0, 0, 1), size=10, **options)
edit flag offensive delete link more

Comments

Thank you vey much ! it works and Ineed to get some new kwoledge out of this ! apprciated.

moon gravatar imagemoon ( 2023-01-07 15:24:34 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-01-07 13:58:18 +0100

Seen: 202 times

Last updated: Jan 07 '23