473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

newton-raphson evaluate

2
Hi,

I am writing a program to find the roots of any polynomial using the newton-rpahson method and I want to be able to give it a polynomial as an argument and then evaluate and differentiate it.

Is there an easy way of doing this (getting all the coefficients and powers in an array?) so an example argument might be x^4+2x^3+5x^2+8x-4=0
and I need a function to evaluate this for a particular value of x and a function to differentiate it...
I had a quick look for expression parsers but I couldn't find much.
Can anyone help?

Thanks,

Richard
Oct 2 '07 #1
5 2237
Laharl
849 Expert 512MB
This is a slightly convoluted method, but it /should/ work.

The following functions will be of use in this, listed by header file:

cstring:
strtok - tokenizer - Google this for examples of use, it's kinda annoying
atoi - converts C-string of digits to an integer

ctype:
isdigit - boolean asking if the character is a digit 0-9

Basically, tokenize the input string with some delimiter, I think you can use multiple, so use + and - to break it up into terms, then x to split coefficients and powers. Then, using isdigit, remove all the non-digit characters and convert to integer with atoi. At this point, you can put the coefficients and powers into a function to differentiate, maybe using an array to store related coefficients and powers in adjacent indices...

EDIT: A simpler method came to me, sort of. After tokenizing for coefficients, put them in an array where their index is the power.
Oct 2 '07 #2
bitdiv
2
Thanks for your help, Is there a way to return two arrays from a function? Maybe return a pointer to an array containing pointers to both arrays?

Is there any c example code I can use for this?

Thanks,

Richard
Oct 2 '07 #3
Ganon11
3,652 Expert 2GB
You can pass both arrays as double pointers (e.g. int **array), and dereference them once every time you wish to use them. This way, you can deal with them like a single pointer, but any changes you make will be reflected back in main(). To call the method, you would use something like:

Expand|Select|Wrap|Line Numbers
  1. int *array;
  2. // Do work to populate the array
  3. myMethod(&array);
Oct 2 '07 #4
arnaudk
424 256MB
You might want to check out GSL which has a large collection of fast and efficient algorithms for numerical root finding.

Arnaud
Oct 2 '07 #5
Hi,

I am writing a program to find the roots of any polynomial using the newton-rpahson method and I want to be able to give it a polynomial as an argument and then evaluate and differentiate it.

Is there an easy way of doing this (getting all the coefficients and powers in an array?) so an example argument might be x^4+2x^3+5x^2+8x-4=0
and I need a function to evaluate this for a particular value of x and a function to differentiate it...
I had a quick look for expression parsers but I couldn't find much.
Can anyone help?

Thanks,

Richard

Is there no way you could use something like Matlab for this? It would be a lot easier.


Anyhow, I'm no expert at C++ -- and I'm not sure if a regular expression library exists or how it's syntax works if it does exist. But if you can figure out this, you can use RegEx to do something like this (I'm basing this on how this works in perl)

result_array = split ( /\W/, expression);

This would split the expression based on any "non word" character, ie, splits based on things other than (0-9 a-z A-z)

So, in your example x^4+2x^3+5x^2+8x^1-4=0 the above code (if compatible with C++) should yield:

result_array[0] = x
result_array[1] = 4
result_array[2] = 2x
result_array[3] = 3
result_array[4] = 5x
result_array[5] = 2
result_array[6] = 8x
result_array[7] = 1
result_array[8] = 4
result_array[9] = 0

From these values, I think it would be easier to figure out the coefficients for the different terms, etc.

Then you could do

result_array2 = split ( /\d/, expression); //splitting based on any digit

For x^4+2x^3+5x^2+8x^1-4=0 this would yield
result_array2[0] = x^
result_array2[1] = +
result_array2[2] = x^
result_array2[3] = +
result_array2[4] = x^
result_array2[5] = +
result_array2[6] = x^
result_array2[7] = -
result_array2[8] = =


The odd elements of this array would tell you the order of additions and subtractions

Note: you would have to use 8x^1 instead of 8x for my above code to work

So yeah, in other words, I've found reg ex really helps when you need to parse something. If you can figure out how to work it with C++ then you could do something similar to the above or (and this is very likely) come up with a better solution than what I did.
Oct 2 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

34
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream>...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
1
by: Luca | last post by:
Hi guys, I am trying to find a way to solve equations using Netwon Rap. method inside Visual Studio. Is there any body that is aware of that? Thanks in advance Luca
7
by: sptutx | last post by:
Write a C program that uses Newton's Method to solve an equation in one variable. Try solving x^x = ln2 the deriviative of x^x is x^x(lnx + 1). The 'ln' function in C is log(), and the...
3
by: Jonas Huckestein | last post by:
Hello, I have the following problem: I have one BaseClass, which defines the virtual functions getValue(int index) and virtually overloads the operators *, /, -, +. Now I want two classes...
11
by: kartikegarg | last post by:
can you help me please with this problem.. i want a c program using newton raphson method for solving 18 equations... the equations are not of degree greater than 1... i need the program to input...
1
by: luuska | last post by:
Hello. This is my first C++ code (and my first post). I've created a code, that calculates the root of function f(x) = sin(x). I use 3 as x_0. Here's the output: Insert the number of...
92
by: ureuffyrtu955 | last post by:
Python is a good programming language, but "Python" is not a good name. First, python also means snake, Monty Python. If we search "python" in google, emule, many results are not programming...
30
by: luvraghu | last post by:
Hi, Can anyone please give me a hint/logic to divide a number with any number without using '/' '+' '*' '-'. Thank You. Regards, New
2
by: ilovecolours | last post by:
hi everyone can u please help me to explain newtons method to minimize logarithmic ( y=a- b (ln x) and parabolic equation? and pleass post its program also.. thank you in advance...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.