• 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
https://www.neowin.net/forum/topic/831266-c-nonlinear-secant-method-program/
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.

  • 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...

  • 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.

  • 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.

  • 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.

  • 0
  DAaaMan64 said:
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.

  • 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..

:(

  • 0
  lady_lyssa said:
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.

  • 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.

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

    • No registered users viewing this page.
  • Posts

    • I have the Tab A9+ also, and I agree with all your points. I really like this tablet.
    • Microsoft changed recall to opt-in some time ago, which seems to still be the case. I personally feel like that is the right move for something like this. You are right, settings that turn themselves back on after an update is totally unacceptable; its weird how that only happened with privacy and advertising related settings...
    • I have a Tab S9 FE and a Tab A9+ which is a lot less expensive. I much prefer the TabA9+. 1. Its display is more landscape while that of the S9 FE is more boxy. 2. They both drain battery at about the same rate of between 7% and 9% an hour depending on how I use them 3. The Tab A9+ charges a lot faster than S9 FE, The S9 FE is better at handling memory, It is faster. They both have 8GB. The TabA9+ does a lot better at handling memory on Android 15, One UI 7.0 than it did on Android 14, One UI 6.1
    • Following rough launch, Splitgate 2 is going back to beta as studio announces layoffs by Pulasthi Ariyasinghe The sequel to the free-to-play arena shooter Splitgate was released just a couple of months ago alongside a surprise Battle Royale mode, but it doesn't look like the launch has gone too well for the studio, 1047 Games. In a lengthy social media post, the developer revealed that it is making some major changes within the company that will also affect its games. The studio said that it agrees with the community regarding Splitgate 2 launching too early with rushed features. Because of this, the title is returning to its beta state. While Splitgate 2 will remain playable, the studio will be deep in development reworking the title behind the scenes until at least early 2026. The soon-landing Season 3 content update is still planned for release. "We're returning not just to our roots in what we build, but in how we build it – with you," said the company. "That means more playtests, more surveys, more listening, and truly being community first. Our goal is to combine the DNA of the first game with the best improvements from the second." The studio also revealed that this shakeup means that it had to cut some team members from the studio. It didn't give an exact number for the roles being cut but said that "we hope to bring them back when we can." In the same vein of keeping costs low, 1047 Games will be shutting down the servers for the original Splitgate within a month's time. "While we'd love to keep servers online indefinitely, it's cost us hundreds of thousands of dollars over the past couple of years, and we have to prioritize our team," added the studio. It said that offline play or peer-to-peer matchmaking for Splitgate are being explored for fans to keep playing, but nothing concrete about these potential options was announced today.
  • Recent Achievements

    • Week One Done
      SmileWorks Dental earned a badge
      Week One Done
    • Community Regular
      vZeroG went up a rank
      Community Regular
    • Collaborator
      Snake Doc earned a badge
      Collaborator
    • Week One Done
      Snake Doc earned a badge
      Week One Done
    • One Month Later
      Johnny Mrkvička earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      587
    2. 2
      Michael Scrip
      199
    3. 3
      ATLien_0
      193
    4. 4
      +FloatingFatMan
      132
    5. 5
      Xenon
      122
  • Tell a friend

    Love Neowin? Tell a friend!