Hi,
Right-clicking on the graph, select "Customize" from the context menu, click the "+" icon. From here you can add a script using the Y function ( ) function, which takes 2 arguments: an expression (the function) and the domain variable (here, x).
Click OK and you're done.
This is also easy to do via scripting with the Y function ( ) function. Here, y = 2x^2 + 3x -5, corresponding to coefficients [2, 3, -5] is plotted. I've used Horner's rule here, as it makes things easier in the next illustration.
dt = astable(J(40,2,randominteger(-20,20)));
gb = dt << Graph Builder( Variables( X( :Col2 ), Y( :Col1 ) ), Elements( Points( X, Y, Legend( 3 ) ) ) );
report(gb)[framebox(1)] << add graphics script ( Y Function( (2 * x + 3) * x - 5, x ) );
One issue is that the coefficients might be determined at runtime, might not be integers, and the degree of the polynomial might be unknown. In this case something like the following will work:
dt = astable(J(40,2,randominteger(-20,20)));
gb = dt << Graph Builder(
Variables( X( :Col2 ), Y( :Col1 ) ),
Elements( Points( X, Y, Legend( 3 ) ) ),
);
// get random uniform coefficient matrix
coef = J(5, 1, randomuniform());
// build an expression using Horner's rule:
for each ( {v, i }, coef,// i = 1; v = coef[i];
if (i > 1,
fctExp = insert ( insert ( expr ( multiply ( ) ), nameExpr ( fctExp ) ), expr ( x ) );
,
fctExp = expr ( 0 );
);
fctExp = insert ( insert ( expr( add ( ) ), nameexpr( fctExp ) ), v );
);
// substitute into a graph script expressison and evaluate
graphAdd = expr (
report(gb)[framebox(1)] << add graphics script ( Y Function( _EXPRESSION_ , x ) )
);
eval ( substitute ( nameexpr(graphAdd), expr( _EXPRESSION_ ), nameexpr ( fctExp ) ) );
//show the expression in the log
show ( fctExp )