Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,550 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,577 people online right now. Registration is fast and FREE... Join Now!




Find square root using Talor Series

 
Reply to this topicStart new topic

Find square root using Talor Series, Find square root using Talor Series

SueJ
23 Nov, 2007 - 03:33 PM
Post #1

New D.I.C Head
*

Joined: 23 Nov, 2007
Posts: 3


My Contributions
Please help, I have tried and tried to make this work like my teacher wants it to work.

I am suppose to write code to find the square root of a number using the Taylor Series, which is:
sqrt(x) = 1 + 1/2(x-1) -
1/4(x-1)^2/2! + 3/8(x-1)^3/3! - 15/16(x-1)^4/4!

This way works.
CODE
#include <stdio.h>
#include <math.h>

int main(void)
{
   double x, n, m = 0.0;
   int i, cnt = 0;

   // Any input
   printf("Enter a number.\n");
   scanf("%lf", &n);
   x = n;
   m = sqrt(n);

   // Divide by 4 until n < 1
   while (n > 1) {
      n = n/4;
      cnt++;
   }

   double mysqrt;
   double x1, x2, x3, x4, x5;

   x1 = 1.0;
   x2 = (n-1)/2;
   x3 = x2*(n-1)/4;
   x4 = x3*(3/8)*(n-1)/(3*2);
   x5 = x4*(15/16)*(n-1)/(4*3*2);

   mysqrt = x1 + x2 - x3 + x4 - x5;

   // Add up the pieces
   float total, xx;
   xx = pow (2,cnt);
   total = mysqrt * xx;

   printf("Library function, the square root of  %.3lf is %.3lf\n", x, m);
   printf("Talor Series, the square  root of %.3lf is %.3lf\n", x, total);

   return 0;
}



But my teacher wants me to use this form.
// sqrt(1-x) = 1 - x/2 - x^2/4 -
.... - {1*3*5...(2n-3)x^n/2*n*n!
Mine does not work
CODE

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
   double x, n, m = 0.0;
   int i, cnt = 0;

   // Any input
   printf("Enter a number.\n");
   scanf("%lf", &n);
   x = n;
   m = sqrt(n);

   // Divide by 4 until n < 1
   while (n > 1) {
      n = n/4;
      cnt++;
   }

   double mysqrt = 1.0;
   double x1 = 1;

  
   for (i=1; i<=5; i++) {
      x1 = 1 * (((2 * i) - 3) * (1-n)) / (2 * i * i);
      mysqrt = mysqrt + x1;

   }

  // Add up the pieces
   if (cnt)
      mysqrt = (cnt * 2) * mysqrt;

   printf("Library function, the square root of %.3lf is %.3lf\n", x, m);
   printf("Talor Series, the square root of %.3lf is %.3lf\n", x, mysqrt);

   return 0;
}


This post has been edited by NickDMax: 23 Nov, 2007 - 04:29 PM
User is offlineProfile CardPM
+Quote Post

mattman059
RE: Find Square Root Using Talor Series
23 Nov, 2007 - 04:02 PM
Post #2

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 361


Dream Kudos: 175
My Contributions
Please use the code brackets so your code is easier to read



This post has been edited by mattman059: 23 Nov, 2007 - 04:03 PM
User is offlineProfile CardPM
+Quote Post

SueJ
RE: Find Square Root Using Talor Series
23 Nov, 2007 - 04:44 PM
Post #3

New D.I.C Head
*

Joined: 23 Nov, 2007
Posts: 3


My Contributions
QUOTE(SueJ @ 23 Nov, 2007 - 04:33 PM) *

Please help, I have tried and tried to make this work like my teacher wants it to work.

I am suppose to write code to find the square root of a number using the Taylor Series, which is:
sqrt(x) = 1 + 1/2(x-1) -
1/4(x-1)^2/2! + 3/8(x-1)^3/3! - 15/16(x-1)^4/4!

This way works.
CODE
#include <stdio.h>
#include <math.h>

int main(void)
{
   double x, n, m = 0.0;
   int i, cnt = 0;

   // Any input
   printf("Enter a number.\n");
   scanf("%lf", &n);
   x = n;
   m = sqrt(n);

   // Divide by 4 until n < 1
   while (n > 1) {
      n = n/4;
      cnt++;
   }

   double mysqrt;
   double x1, x2, x3, x4, x5;

   x1 = 1.0;
   x2 = (n-1)/2;
   x3 = x2*(n-1)/4;
   x4 = x3*(3/8)*(n-1)/(3*2);
   x5 = x4*(15/16)*(n-1)/(4*3*2);

   mysqrt = x1 + x2 - x3 + x4 - x5;

   // Add up the pieces
   float total, xx;
   xx = pow (2,cnt);
   total = mysqrt * xx;

   printf("Library function, the square root of  %.3lf is %.3lf\n", x, m);
   printf("Talor Series, the square  root of %.3lf is %.3lf\n", x, total);

   return 0;
}



But my teacher wants me to use this form.
// sqrt(1-x) = 1 - x/2 - x^2/4 -
.... - {1*3*5...(2n-3)x^n/2*n*n!
Mine does not work
CODE

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
   double x, n, m = 0.0;
   int i, cnt = 0;

   // Any input
   printf("Enter a number.\n");
   scanf("%lf", &n);
   x = n;
   m = sqrt(n);

   // Divide by 4 until n < 1
   while (n > 1) {
      n = n/4;
      cnt++;
   }

   double mysqrt = 1.0;
   double x1 = 1;

  
   for (i=1; i<=5; i++) {
      x1 = 1 * (((2 * i) - 3) * (1-n)) / (2 * i * i);
      mysqrt = mysqrt + x1;

   }

  // Add up the pieces
   if (cnt)
      mysqrt = (cnt * 2) * mysqrt;

   printf("Library function, the square root of %.3lf is %.3lf\n", x, m);
   printf("Talor Series, the square root of %.3lf is %.3lf\n", x, mysqrt);

   return 0;
}




I found my problem. Thanks anyway.
This is the working version





Attached File(s)
Attached File  squareFinal.txt ( 1.11k ) Number of downloads: 61
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:46PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month