• 0

C++ nonlinear secant method program


Question

hi, im a beginner in using C++...

i have a project due in a few days and i've been trying to do the program for a week now... we're supposed to make a program that would allow the user to enter any polynomial equation (always = 0), asks the user the interval and number of iterations and solve it using secant method...

for example, the user entered the polynomial equation of 0.5x^4 + 2x^2 - x + 4 = 0, interval [-0.25, 0], no of iterations is 5, the result should be the following:

339qefn.jpg

the screen should look like this when program is run:

3325hs4.jpg

i already have a code for the secant method formula part for computing the new x sub n, however i don't have a clue as to how i would make a code enabling the user to enter the polynomial equation to be solved and substituting the new x (which is the new computed x sub n obtained from the secant formula) to the polynomial equation?

the question is what I want possible?... is there any kind of library that I need for wat I want?..

hope u guys can help coz honestly im no good in programming? I'm getting crazy with this project coz due is in 2 days time (sobs)? please help?

for additional information, for the secant part of the program? the code is the following:

for(int i=1;i<=n;i++)
{
	 c[i+1]=c[i]-(f(c[i]) * (c[i]-c[i-1])) / (f(c[i])-f(c[i-1]));
	 cout <<"c(" << i+1 <<") = "<< c[i+1] <<"\n";
}

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

lol that was like the 4th google result: http://www.programmersheaven.com/mb/beginn...d-programming-/

Maybe you should just try to push through this? If you don't understand how to use std::cin and std::cout you probably just need to hit the books a little and practice. You'll never been good without those few basics. 2 days should be plenty of time to figure out simple I/O on your own.

std::cin is real easy to use.

something like:

int first_variable;

char first_math_operator;

int next_variable;

std::cin >> first_variable;

>> first_math_operator;

>> next_variable;

etc.

Good luck, study hard.

Link to comment
Share on other sites

  • 0

@DAaaMan64 : i know how to use those... my problem is in the polynomials part...

i have a code here that i made that does wat i want with the exception of entering the polynomial equation... this program solves the equation x - 2 + ln x = 0... however i dont know how to construct the part wherein the program could do the number of iterations that the user wants... i hope this gives you an idea...

#include <stdio.h>
#include <math.h>
double f(double);
int main()
{
	double k, x1, x2;
	int n;
	double y;

	printf("Enter the interval [x1,x2]: ");
	scanf("%lf, %lf", &x2, &x1);


	printf("Checking the boundaries:\n");
	printf("Xn\t\tf(Xn)\n");
	printf("%.6lf\t%.6lf\n", x1, f(x1));
	printf("%.6lf\t%.6lf\n", x2, f(x2));

	for (n = 1; n<=4; n++)
	{
		y = (x1 - x2) / (f(x1) - f(x2)) * f(x1);
		x2 = x1;
		x1 = x1 - y;
		printf("%.6lf\t%.6lf\n", x1, f(x1));
	}


	scanf("%lf", &k);
	return 0;
}

double f(double x)
{   
	return((x-2)+log(x));
}

problem is the part wherein the user wud enter the polynomial equation to be evaluated... maybe i should explain what the program should do... (i used the example in my first post...)

1) the polynomial equation that the user will enter will be the f(x) which is always equal to 0 (hence the example equation i used before was equal to 0)

2) the interval that the user will enter is the 1st x and 2nd x (respectively) which the program will evaluate giving a new value that will be shown under the column of f(x sub n)

[in the example, the interval was [-0.25 , 0] and these were substituted to the polynomial equation thus giving the values of 3.876953 (for x=-0.25) and 4 (for x=0)]

3) the new x that will be used for evaluation is obtained through the secant formula which is:

1zfsbhc.jpg

[in the example, the 3rd x in the column under x sub n is obtained through the secant formula:

o7j6s7.jpg

this new x will be evaluated (substituting it to the polynomial equation and getting a value of 2325.374361]

4) the 3rd step is repeated until the number of iterations is satisfied (in the example, the intervals, which are -0.25 and 0 are iteration 0 and iteration 1 respectively)

please if u cud, help me... i wud really really appreciate it...

Link to comment
Share on other sites

  • 0

I don't get what you're asking for. You say you don't know how to "enable the user to enter the polynomial equation to be solved". That's just basic C++ I/O. Using cin for each number the user must enter, this way:

double a, b, c;

cin >> a;

cin >> b;

cin >> c;

Then you can use a, b, c as coefficients (or whatever) in the polynomial equation to be solved.

Link to comment
Share on other sites

  • 0

Yeah that would be the best way to do it. Then each time the user enters a number it increases the power by 1, so first is x^0, then x^1 etc, and then add a character that the user can enter to break from the loop.

Then you can just substitute the numbers into the code that you have.

Link to comment
Share on other sites

  • 0

If you need the ability to enter an infinite number of variables, just use a std::vector. That way you can just do something like this:

std::vector<char> variables;

char temp;

while(std::cin)

{

std::cin >> temp;

variables.push_back(temp);

}

A vector is an array on crack, and when that vector fills itself up, it will auto expand. This is quite an inefficient method, but whatev.

Link to comment
Share on other sites

  • 0
If you need the ability to enter an infinite number of variables, just use a std::vector. That way you can just do something like this:

std::vector<char> variables;

char temp;

while(std::cin)

{

std::cin >> temp;

variables.push_back(temp);

}

A vector is an array on crack...

Haha nice expression :p Next time I'm using C++ I'll have to have a look at this vector malarki.

Link to comment
Share on other sites

  • 0

maybe i should tell u, im only just teaching myself how to use C++... we weren't really taught that subject... this project is actually for another subject (a mathematical one, nothing to do with programming, the professor just happen to know programming thus the project)...

so u see, i literally don't know much about c++.. which is the reason why i'm asking questions that for u might be easy to answer... that's why there are forums like these right... to help people like me who dont know much about programming... sorry but i just wanna point that out..

:(

Link to comment
Share on other sites

  • 0
maybe i should tell u, im only just teaching myself how to use C++... we weren't really taught that subject... this project is actually for another subject (a mathematical one, nothing to do with programming, the professor just happen to know programming thus the project)...

so u see, i literally don't know much about c++.. which is the reason why i'm asking questions that for u might be easy to answer... that's why there are forums like these right... to help people like me who dont know much about programming... sorry but i just wanna point that out..

:(

Okay well we've given you significant answers. I hope they help. Now some minor forum advice, please try to use capitols at the beginning of sentences and lose the constant "...". It bothers readers, especially when you need help. If you really want to kick ass, stop abbreviating too, none of this 'u' stuff.

Oh and for programming, google is a much better buddy then forums. If google doesn't do it, then use IRC, and if IRC doesn't do it, then finally use forums.

Link to comment
Share on other sites

  • 0

Yeah agree with what you're saying DAaaMan64, searching on Google is probably the best way of finding out answers. 99 times out of a 100 someone has asked it first, or there's a help page. Not only that, it teaches you to look for information which imo is a very important skill.

Also to the OP. The code that you have posted is actually written in C, not C++. You could try giving 'secant method C' a search in Google. There's maybe some example code you can modify.

So if you would rather code it in C, you will be using scanf instead of cin, and printf instead of cout. Then you can use Dr_Asik's method for data entry.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.