POV-Ray : Newsgroups : povray.general : Roots of a function : Re: Roots of a function Server Time
25 Apr 2024 13:37:10 EDT (-0400)
  Re: Roots of a function  
From: IGM
Date: 29 Sep 2019 03:10:01
Message: <web.5d905827b127c26fe462584c0@news.povray.org>
This is my poor-man solution:

// root(s) of function F = Y(X)-thr
// Example:
// #declare X = array [10] {0,10,20,30,40,50,60,70,80,90};
// #declare C = array [10]
{0.000,21.000,44.000,69.000,97.000,124.000,158.000,181.000,206.000,232.000};
// root(X,C,35)
// #debug concat("root: ", str(macro_value,5,3),"\n")
#macro root(X,Y,thr)

    #include "math.inc"
    GetStats(Y);

    #local f_prism = prism {
        linear_spline
        -1, 1, dimension_size(Y,1)+3,

        #for (k,0,dimension_size(Y,1)-1)
            <X[k],Y[k]>,
        #end
        <X[dimension_size(Y,1)-1],StatisticsArray[2]-1>, // add lower point 1
        <X[0],StatisticsArray[2]-1>, // add lower point 2
        <X[0],Y[0]> // close prism
        // rotate x*-90 // For plot in x,z plane. In this case P0 = <0,thr,0>
        }

    #object {f_prism pigment {rgb <0,2,0>} } // no_image?

    #local P0 = <0,0,thr>; // Starting point
    #local V0 = <1,0,0>; // Incident vector
    // Trace to object
    #local N = <99,99,99>; // init
    #local P1 = trace(f_prism, P0, V0, N); // Loop here to find multiple roots
    // Plot traced ray:
    // #cylinder {P0, P1, 1 pigment {rgb <0,1,0>} no_shadow no_reflection}

    // For multiple roots create array!
    #declare macro_value = P1.x;
#end


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.