## Concept of local variables in (wx)maxima programs document.SUBSCRIPTION_OPTIONS = { "thing": "thread", "subscribed": false, "url": "subscribe", "icon": { "css": "fa fa-envelope-o" } };

2014-03-04
2014-03-10
• Wolfgang Meiners - 2014-03-04

It seems, i have problems to understand the concept of local variables in wxmaxima.

I am just learning to write wxmaxima programs and the following is one of the first programs i have written:

=====================================================================================
/ [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]/
/ [ Created with wxMaxima version 12.04.0 ] /

/ [wxMaxima: comment start ]
Given an List with three Points, Arr: [[x1,y1],[x2,y2],[x3,y3]] find
a polynomial f(x) of max. degree 2 through this points,
f(xi)=yi (i=1,2,3)

[wxMaxima: comment end ]
/

/ [wxMaxima: comment start ]
The following solution fails. How is this done correctly?

[wxMaxima: comment end ]
/

/ [wxMaxima: input start ] /
kill(all);
/ [wxMaxima: input end ] /

/ [wxMaxima: input start ] /
block(
[expr, EQN, SOL, a,b,c],
expr: avar^2 + bvar + c,
EQN: map(lambda([e], subst(var=e[1],expr)=e[2]),Arr),
/ uncomment the following line to see the equations /
/ print(EQN), /
SOL: linsolve(EQN,[a,b,c]),
return(subst(SOL,expr))
)\$
/ [wxMaxima: input end ] /

/ [wxMaxima: comment start ]
This is ok:

[wxMaxima: comment end ]
/

/ [wxMaxima: input start ] /
/ [wxMaxima: input end ] /

/ [wxMaxima: input start ] /
map(f,[1,2,3]);
/ [wxMaxima: input end ] /

/ [wxMaxima: comment start ]
This is also ok:

[wxMaxima: comment end ]
/

/ [wxMaxima: input start ] /
/ [wxMaxima: input end ] /

/ [wxMaxima: input start ] /
map(f,[x1,x2,(x1+x2)/2]),expand,factor;
/ [wxMaxima: input end ] /

/ [wxMaxima: comment start ]
This is Simpsons rule:

[wxMaxima: comment end ]
/

/ [wxMaxima: input start ] /
integrate(f(x),x,x1,x2),factor;
/ [wxMaxima: input end ] /

/ [wxMaxima: comment start ]
And this is ok, too:

[wxMaxima: comment end ]
/

/ [wxMaxima: input start ] /
f(x):= rx^3+sx^2+tx+u;
/
[wxMaxima: input end ] */

/* [wxMaxima: comment start ]

[wxMaxima: comment end ] */

/ [wxMaxima: input start ] /
map(lambda([x], g(x)-f(x)),[x1,x2,(x1+x2)/2]),expand,factor;
/ [wxMaxima: input end ] /

/ [wxMaxima: comment start ]
This fails:

[wxMaxima: comment end ]
/

/ [wxMaxima: input start ] /
f(x):= ax^3+bx^2+cx+d;
/
[wxMaxima: input end ] */

/ [wxMaxima: input start ] /
map(lambda([x], g(x)-f(x)),[x1,x2,(x1+x2)/2]),expand,factor;
/ [wxMaxima: input end ] /

/ [wxMaxima: comment start ]
The failure is due to the global variables a,b,c which lead to a messed system of equations.
To see the resulting equations, uncomment the print-statement in QuadApprox.

[wxMaxima: comment end ]
/

/ Maxima can't load/batch files which end with a comment! /
"Created with wxMaxima"\$

=====================================================================================

• Wolfgang Meiners - 2014-03-05

I am really sorry for the messes code i posted. I was not aware of the automatic formatting that took place. Here i give it another try:

The following is code i have written to learn programming (wx)maxima. By playing with it i found an failure if there are global variables a,b,c. The code does run in my version of wxmaxima.

```/*
Given an List with three Points, Arr: [[x1,y1],[x2,y2],[x3,y3]] find
a polynomial f(x) of max. degree 2 through this points,
f(xi)=yi (i=1,2,3)
*/

/*
The following solution fails. How is this done correctly?
*/

kill(all);

block(
[expr, EQN, SOL, a,b,c],
expr: a*var^2 + b*var + c,
EQN: map(lambda([e], subst(var=e[1],expr)=e[2]),Arr),
/* uncomment the following line to see the equations */
/* print(EQN), */
SOL: linsolve(EQN,[a,b,c]),
return(subst(SOL,expr))
)\$

/*
This is ok:
*/

map(f,[1,2,3]);

/*
This is also ok:
*/

map(f,[x1,x2,(x1+x2)/2]),expand,factor;

/*
This is Simpsons rule:
*/

integrate(f(x),x,x1,x2),factor;

/*
And this is ok, too:
*/

f(x):= r*x^3+s*x^2+t*x+u;

/*

*/

map(lambda([x], g(x)-f(x)),[x1,x2,(x1+x2)/2]),expand,factor;

/*
This fails:
*/

f(x):= a*x^3+b*x^2+c*x+d;

map(lambda([x], g(x)-f(x)),[x1,x2,(x1+x2)/2]),expand,factor;

/*
The failure is due to the global variables a,b,c which lead to a messed system of equations.
To see the resulting equations, uncomment the print-statement in QuadApprox.
*/
```

So if anyone can give me a hint, how to avoid this kind of problem, i would be very glad. Thank you for any hint.

Wolfgang

• Wolfgang Meiners - 2014-03-10

So i eventually found a solution:

```QuadApprox(Arr,var):=
block(
[expr, eqn, sol, a, r],
expr: sum('a[i]*var^i,i,0,2),
r: map(lambda([e],e[2]),Arr),
eqn: map(lambda(['e,'b], subst(var=e[1],expr)=b),Arr,r),
sol: linsolve(eqn,makelist('a[i],i,0,2)),
/* print(expr,eqn, a,r,ev(sol,nouns)), */
return(ev(subst(sol,expr),nouns))
)\$
```

seems to do what i expect.