381 - A10 - Splines

391 days ago by Professor381

# Using the Matlab builtin function x = [0 1 2.5 3.6 5 7 8.1 10]; y = sin(x); xx = 0:.05:10; yy = spline(x,y,xx); plot(x,y,'o',xx,yy) 
       
                                                                        

    1
|--------------------------------------------------------------------|  

      |       ##  ####            +            +         ##  + ###      
|   
      |     ##        #                                ##         #     
|   
      |    #           #                              #           ##    
|   
      |   #            #                              #             #   
|   
  0.5 |-+#              #                            #               #  
+-|   
      |  #               #                          #                 # 
|   
      | #                 #                         #                  #
|   
      |#                  #                        #                   #
|   
      |#                   #                      #                    
#  |   
    0 |-+                   #                    #                     
#+-|   
      |                     #                   #                       
# |   
      |                      #                  #                       
# |   
      |                      #                 #                        
#|   
      |                       #F               #                        
#|   
 -0.5 |-+                      #              #                         
+-|   
      |                         #            #                          
|   
      |                          #          #                           
|   
      |                           #        #                            
|   
      |             +             +##     #    +             +          
|   
   -1
|--------------------------------------------------------------------|  

      0             2             4            6             8          
10   
                                                                        
                                                                               
    1 |--------------------------------------------------------------------|   
      |       ##  ####            +            +         ##  + ###         |   
      |     ##        #                                ##         #        |   
      |    #           #                              #           ##       |   
      |   #            #                              #             #      |   
  0.5 |-+#              #                            #               #   +-|   
      |  #               #                          #                 #    |   
      | #                 #                         #                  #   |   
      |#                  #                        #                   #   |   
      |#                   #                      #                     #  |   
    0 |-+                   #                    #                      #+-|   
      |                     #                   #                        # |   
      |                      #                  #                        # |   
      |                      #                 #                          #|   
      |                       #F               #                          #|   
 -0.5 |-+                      #              #                          +-|   
      |                         #            #                             |   
      |                          #          #                              |   
      |                           #        #                               |   
      |             +             +##     #    +             +             |   
   -1 |--------------------------------------------------------------------|   
      0             2             4            6             8            10   
                                                                               
# Now, let's do this parametrically # Using the Matlab builtin function t = [0 1 2 5 7 8 10]; x = [0 4 4 3 2 -1 0]; y = [0 0 3 5 0 -3 1]; tt = 0:.05:10; xx = spline(t,x,tt); yy = spline(t,y,tt); plot(x,y,'o',xx,yy) 
       
Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax
Traceback (most recent call last):    xx = spline(t,x,tt);
  File "", line 1, in <module>
    
  File "/tmp/tmpQgFDuR/___code___.py", line 4
    t = [_sage_const_0  _sage_const_1  _sage_const_2  _sage_const_5  _sage_const_7  _sage_const_8  _sage_const_10 ];
                                    ^
SyntaxError: invalid syntax
# Using Sage for a functional set of points xs = [0, 1, 2.5, 3.6, 5, 7, 8.1, 10] ys = [0, 1, 2.5, 3.6, 5, 7, 8.1, 10] pts = [] for k in range(len(xs)): ys[k] = sin(xs[k]) # comment this out if you want to just use the y-values entered above or change to another formula to model with cubic pieces pts.append((xs[k],ys[k])) end f = spline(pts) G = plot(f,(x,0,10)) G += points(pts,size=20,color='red') show(G) 
       
var('t') ts = [0, 1, 2, 5, 7, 8, 10] xs = [0, 4, 4, 3, 2, -1, 0] ys = [0, 0, 3, 5, 0, -3, 1] pts = [] ptsx = [] ptsy = [] for k in range(len(xs)): pts.append((xs[k],ys[k])) ptsx.append((ts[k],xs[k])) ptsy.append((ts[k],ys[k])) end f = spline(ptsx) h = spline(ptsy) G = parametric_plot((f,h),(t,min(ts),max(ts))) G += points(pts,size=20,color='red') show(G)