Project 1 will be to implement a finite element method for a two-point boundary-value problem. It will
have several parts.

The quadratic formula says that the solutions of ax 2 + bx + c = 0 are given by

If b2 − 4ac < 0 then √ is imaginary, so there is no problem with round-off error.
If b2 − 4ac > 0 then cancellation can occur in if b > 0 and in if b < 0.
Thus, if b > 0 one would want to use the computations

Similarly if b < 0.
Write a function (quadratic-solver a b c) that returns a list of the two roots of ax 2 + bx + c = 0 as
accurately as possible. Test your code on the following problems:

euler-126% gsi
Gambit v4.1.2
> (sqrt -1)
+i
> (sqrt +i)
.7071067811865476+.7071067811865475i
(-1+2i -1-2i)
(1/2+.8660254037844386i 1/2-.8660254037844386i)
(-2.414213562373095 .4142135623730951)
(-1/8+.4841229182759271i -1/8-.4841229182759271i)
(-1/2 -1/2)
(+1/2i -1/2i)

*** ERROR IN (console)@11.1 -- not a quadratic: 0 0 1

1>
(-1/2 1/2)
(-3.138428376721e12 -3.186308177103568e-13)
>

*** EOF again to exit

Meroon

Standard Scheme (so-called R5RS Scheme, which Gambit implements) does not have an object system.
We use an object system provided by the software package Meroon.

To use Gambit, you need to have /pkgs/Gambit-C/current/bin/ in your path. The Gambit interpreter
is called gsi and the Gambit compiler is called gsc.

To have Gambit load Meroon automatically, just call gsi++ or gsc++.

Our system has two differences with standard Meroon:

(1) In standard Meroon, keywords begin with a colon; in our Meroon keywords end with a colon:
(define-class Polynomial Object
((= variable immutable:)
(= terms immutable:)))

(2) In standard Meroon, so-called setters begin with set- and end with !. In our Meroon, setters end
with -set!:

euler-130% gsi++
[ Meroon V3 Paques2001+1 \$Revision: 1.1 \$ ]
Gambit v4.1.2
> (define-class Point Object (x y))
Point
> (define p (make-Point 0 1))
> (unveil p)
(a Point <------------- [Id: 1]
x: 0
y: 1 end Point)
#t
> (Point-x-set! p 1)
#<meroon #2>
> (unveil p)
(a Point <------------- [Id: 1]
x: 1
y: 1 end Point)
#t
>

Numerical Integration

The Gauss-Lobatto quadrature rules with n points have the form

Here are the zeros of the degree n − 2 orthogonal polynomial over [−1, 1] with the weight

If we define

then has degree n − 1 and satisfies

The weights satisfy

So, the first part of the project is to write code to manipulate polynomials.
and modify it to use Meroon’s framework of classes/objects and generics/methods.

We’ll define a polynomial class:

(define-class Polynomial Object
((= variable immutable:)
(= terms immutable:)))

and a way to check whether two Polynomial variables are the same:

(define (Polynomial-variable= var1 var2)

(eq? var1 var2))

The terms of a polynomial is just a list of nonzero terms, in decreasing order by degree (unfortunately called
“order” at that web page), so we need some code to manipulate terms and lists of terms:

;;; a term is a pair (coeff order) (order should really be degree , but ...)
;;; (Polynomial-terms p) is a list of terms in decreasing orders.
;; ope ration on terms and term-lists
(if (=zero? (term-coeff term))
term-list
(cons term term-list)))
(define (the-empty-termlist)
’())
(define (first-term term-list)
(car term-list))
(define (rest-terms term-list)
(cdr term-list))
(define (empty-termlist? term-list)
(null? term-list))
(define (make-term order coeff)
(list order coeff))
(define (term-order term)
(car term))
(define (term-coeff term)

The web page has code for adding two polynomials . Putting it into our terms we define a generic function

(if (and (number? x)
(number? y))
(+ x y)
(error "add: This generic is not defined on these objects: " x y)))

and then we define a method for adding Polynomials:

(cond ((number? p_2)
(add p1 (number->Polynomial p2 (Polynomial-variable p1))))
((and (Polynomial? p_2)
(Polynomial-variable= (Polynomial-variable p_1)
(Polynomial-variable p_2)))
(instantiate Polynomial
variable: (Polynomial-variable p_1)
(Polynomial-terms p_2))))
(else
(error "add: p_2 is neither a number nor a polynomial with the same variable as
p_1 " p_1 p_2))))

This method is called only when p_1 is a polynomial; if p_2 is a number, it converts p _2 to a Polynomial
with the same variable as p_1 and calls add again with both arguments now a Polynomial.

The web page has code for add-terms:

(cond ((empty-termlist? l1) l2)
((empty-termlist? l2) l1)
(else
(let ((t1 (first-term l1))
(t2 (first-term l2)))
(cond ((> (term-order t1)
(term-order t2))
((< (term-order t1)
(term-order t2))
(else
(make-term (term-order t1)
(term-coeff t2)))
(rest-terms l2)))))))))

So you need to define number->Polynomial, which takes two arguments.

You need to define a multiply generic that works with numbers by default, and a method for multiply
that works on Polynomials; follow ths same pattern as for add. The web page has the guts of the code:
(define (multiply-terms l1 l2)

(if (empty-termlist? l1)
(the-empty-termlist)
(multiply-terms (rest-terms l1) l2))))
(define (multiply-term-by-all-terms t1 L)
(if (empty-termlist? L)
(the-empty-termlist)
(let ((t2 (first-term L)))
(make-term (+ (term-order t1)
(term-order t2))
(multiply (term-coeff t1)
(term-coeff t2)))
(multiply-term-by-all-terms t1 (rest-terms L))))))

So that’s pretty much the code that comes on the web page. Meroon defines a generic function show
that we can specialize for Polynomials as such:

(define-method (show (p Polynomial) . stream)
(let ((port (if (null? stream)
(current-output-port)
(car stream))))
(if (=zero? p)
(display 0)
(show-terms (Polynomial-variable p)
(Polynomial-terms p)
port))
(newline port)))
(define (show-terms variable terms port)
(show-first-term variable (first-term terms) port)
(for-each (lambda (term)
(show-term variable term port))
(rest-terms terms)))
(define (show-first-term variable term port)
(let ((coeff (term-coeff term))
(order (term-order term)))
(display (list (if (and (= coeff 1)
(positive? order))
’()
coeff)
(cond ((zero? order)’())
((= order 1) variable)
(else
(list variable "^" order)))))))
(define (show-term variable term port)
(let ((coeff (term-coeff term))
(order (term-order term)))
(display (list (if (negative? coeff)
coeff
(list "+" coeff))
(cond ((zero? order)’())
((= order 1) variable)
(else
(list variable "^" order))))
port)))

So, here are some problems.
(1) The above code uses a function =zero?. Define a generic function =zero? that handles numbers.
Define a method that works with Polynomials.

(2) Define a generic function (negate (x)) that handles numbers by default. Define a method for
negate that works with Polynomials. Use the generic negate to define a regular function ( subtract
x y).

(3) Define a function (exponentiate x n) that uses multiply to exponentiate anything that multiply
can multiply. Use the discussion of exponentiation on page

(4) Define a function (variable->Polynomial x) that takes a symbol x and returns a Polynomial that
represents the polynomial x, i.e., a single term with coefficient 1 and order 1.

(5) Define a generic function (evaluate f x) that evaluates the function f at x. If f is a number,
assume that it means a function that constantly returns f. Define a method for Polynomials.

If you’ve done the exercises until now, something like the following should work.
;;; evaluation
(define-generic (evaluate (f) x)
(if (number? f)
f
(error "evaluate: unknown argument types " f x)))
(define-method (evaluate (p Polynomial) x)
(evaluate-terms (Polynomial-terms p) x))
(define (evaluate-terms terms x)
(if (empty-termlist? terms)
0
(evaluate-terms (rest-terms terms) x))))
(define (evaluate-term term x)
(multiply (exponentiate x (term-order term))
(term-coeff term)))

Can you write a method that uses Horner’s rule for evaluating Polynomials in our representation?

 Prev Next

Start solving your Algebra Problems in next 5 minutes!

2Checkout.com is an authorized reseller
of goods provided by Sofmath

Attention: We are currently running a special promotional offer for Algebra-Answer.com visitors -- if you order Algebra Helper by midnight of February 19th you will pay only \$39.99 instead of our regular price of \$74.99 -- this is \$35 in savings ! In order to take advantage of this offer, you need to order by clicking on one of the buttons on the left, not through our regular order page.

If you order now you will also receive 30 minute live session from tutor.com for a 1\$!

You Will Learn Algebra Better - Guaranteed!

Just take a look how incredibly simple Algebra Helper is:

Step 1 : Enter your homework problem in an easy WYSIWYG (What you see is what you get) algebra editor:

Step 2 : Let Algebra Helper solve it:

Step 3 : Ask for an explanation for the steps you don't understand:

Algebra Helper can solve problems in all the following areas:

• simplification of algebraic expressions (operations with polynomials (simplifying, degree, synthetic division...), exponential expressions, fractions and roots (radicals), absolute values)
• factoring and expanding expressions
• finding LCM and GCF
• (simplifying, rationalizing complex denominators...)
• solving linear, quadratic and many other equations and inequalities (including basic logarithmic and exponential equations)
• solving a system of two and three linear equations (including Cramer's rule)
• graphing curves (lines, parabolas, hyperbolas, circles, ellipses, equation and inequality solutions)
• graphing general functions
• operations with functions (composition, inverse, range, domain...)
• simplifying logarithms
• basic geometry and trigonometry (similarity, calculating trig functions, right triangle...)
• arithmetic and other pre-algebra topics (ratios, proportions, measurements...)

ORDER NOW!