• 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

    • Hello, Given the reports of Chinese Mini PCs shipping with malware, I would recommend wiping the machine and performing a clean install of Windows on it before use.  From what I can infer from the reports, the Mini PCs that shipped with malware were not the result of targeted purposeful action on the part of the device manufacturers (which is something that has happened with low-cost Android smartphones and TV boxes from China) but rather due to lax security in the manufacturing process.   Getting back to the subject at hand, there are a few steps you will want to go through before wiping the Mini PC: The first things you can do begin before the Mini PC has even arrived.  Once you have ordered it, and know the brand and model, go to that manufacturer's website and download all of the latest device drivers, BIOS (UEFI) firmware updates, machine-specific software (if any), and manuals.  Many Mini PC manufacturers do not do a lot of customization of their device drivers, just shipping whatever device drivers the the silicon vendors provide.  I still recommend downloading them, though, just in case there are some customizations or for initial install since those are the drivers you know the manufacturer validated for the Mini PC.  Store these in a safe place, so you have them ready when the Mini PC arrives. Use Microsoft's Windows Media Creation Tool to create an installation USB.  You can also create a directory on installation USB--like C:\DRIVERS\ or whatnot--and store the extracted device drivers there in case you need them while or after installing Windows. Once the Mini PC arrives, and you have your Windows installation USB available, you can proceed with wiping the PC and doing the clean install.  Here's how you do that, step-by-step: Check the computer and make sure you know how to boot it from a USB flash drive (may be a specific key you have to press when the computer is powered on, or a change to the BIOS (UEFI) firmware settings.  The PC may tell you what key combination you need to press to boot from another drive, or the manual for the PC may it. Plug the USB flash drive into the computer and power it up using the means to have it boot from the Windows install USB. Once the computer finishes booting, it should be at a Windows installation screen. Do not agree to any prompts, copyright licenses, or click on any buttons. Press the Shift + F10 keys together to open a Command Prompt. Run DISKPART to start the command-line disk partitioning utility. The command line prompt will change to DISKPART>. At the DISKPART> prompt, type LIST DISK to get the numbers of all drives installed in the system. Make a note of what number is assigned to what drive (if the Mini PC has more than one drive).  At the DISKPART> prompt, type SEL DISK n  where n is the number of the drive containing Windows. At the DISKPART> prompt, type CLEAN and this will erase the GPT/MBR code from the beginning of the drive. *WARNING:* After performing the clean operation, the drive now be blank/erased, and everything on it will be gone (all files, etc.).  You can exit DiskPart and just continue with the Windows installation as you normally would.  If needed, you can install the device drivers you put on the Windows install media to get your network connection up and running, and from there run Windows Update to get the operating system and device drivers up to date Regards, Aryeh Goretsky
    • Why? Amazon has some great shows and Fallout was near perfect.
    • Both a game adaptation and it's on Amazon I'll set my hopes low
    • You shut your mouth you young fart, that was just a few years ago, lol.
  • Recent Achievements

    • Week One Done
      cac1lll earned a badge
      Week One Done
    • One Month Later
      Falcon.ai earned a badge
      One Month Later
    • Week One Done
      Falcon.ai earned a badge
      Week One Done
    • Dedicated
      EYEREX earned a badge
      Dedicated
    • First Post
      Electronic Person earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      628
    2. 2
      ATLien_0
      238
    3. 3
      Xenon
      166
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!