Quintic - cin >> a...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <cmath> #include <cassert> using namespace std; // Computes ax^5 + bx^4 + cx^3 + dx^2 + ex + f double quintic(double a, double b, double c, double d, double e, double f, double x) { double sum = a; sum *= x; sum += b; sum *= x; sum += c; sum *= x; sum += d; sum *= x; sum += e; sum *= x; sum += f; return sum; } double deriv_quintic(double a, double b, double c, double d, double e, double f, double x) { return quintic(0, 5*a, 4*b, 3*c, 2*d, e, x); } int main() { cout << "Finding a root of ax^5 + bx^4 + cx^3 + dx^2 + ex + f" << endl << endl; double a, b, c, d, e, f, x; cout << "Enter values for a b c d e f (space separated): " << endl;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cin &gt;&gt; a &gt;&gt; b &gt;&gt; c &gt;&gt; d &gt;&gt; e &gt;&gt; f; cout &lt;&lt; &quot;Enter a guess: &quot;; cin &gt;&gt; x; assert(cin); cout &lt;&lt; &quot;f(x) = &quot; &lt;&lt; quintic(a,b,c,d,e,f,x) &lt;&lt; endl; cout &lt;&lt; &quot;f'(c) = &quot; &lt;&lt; deriv_quintic(a,b,c,d,e,f,x) &lt;&lt; endl; double old_x; do { old_x = x; x -= quintic(a,b,c,d,e,f,x) / deriv_quintic(a,b,c,d,e,f,x); } while(fabs(x - old_x) &gt; 0.0001); cout &lt;&lt; &quot;zero: &quot; &lt;&lt; x &lt;&lt; endl; cout &lt;&lt; &quot;f(x) = &quot; &lt;&lt; quintic(a,b,c,d,e,f,x) &lt;&lt; endl; } // (x-4)(x+1)(x-3)(x+6)(x-1) // = x^5 - x^4 - 31x^3 + 73x^2 + 30x - 72 // 1 -1 -31 73 30 -72...
View Full Document

Page1 / 2

Quintic - cin &amp;amp;gt;&amp;amp;gt; a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online