Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

plot scattered data

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. 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

plot scattered data

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