Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Numerical Methods: Rectangle Rule and Trapezoid Rule coding in C using a VI editor and Linux. I don't even know what I'm doing wrong with
Numerical Methods: Rectangle Rule and Trapezoid Rule coding in C using a VI editor and Linux. I don't even know what I'm doing wrong with my lines of code, if you could just help me with what I should be including in the section I would be grateful to learn from my mistakes. (Code included at the bottom).
This is the code:
#include#include #include int main(argc, argv) int argc; char **argv; { double x; double trapezoid(); double rectangle(); int number_of_intervals; double answer; double error; double error_percentage; // the following variables are fixed for this problem double upper_limit=1.0; double lower_limit=0.0; double real_value= 15.0/16.0; // real answer (from analytical portion). // This is used for error calculation. // The following 7 lines reads the number of intervals from the command line // and prints that information to screen. if (argc 5. Numerical. Ok, we've seen that n=4 is not enough intervals to provide a decent answer for this integral, so we'll have to let the computer do this one for us. Step 1: I have done most of the code writing for you. You will only need to write a little bit of code within the larger code that we provide. First, copy my starter code over to your directory The code is available from the course webpage (in the homework section) and is titled: StarterCode.ch Once it is copied over to your Linux directory, you should rename it to something else. Step 2: Compile and run the code. You should get the following output: You forgot to enter the number of intervals You got this message because I set this code up to accept an integer on the command line that tells the code to break the integral up into N intervals. Try to run the code again, entering the number 4 on the command line. For example: a.exe 4 Now, you should get the following output. You have chosen to break the function into 4 intervals Rectangle Rule (n=4): Answer: 0.28203 (Error 69.9163%) Trapezoid Rule (n=4): Answer: -99999.00000 (Error 10666660.0000%) Step 4: Write your own code within function trapezoid. You will see that I left the following comments within function trapezoid: // START OF SECTION THAT YOU MUST WRITE // (-99999 is just a dummy number for "value" until you replace it with something meaningful) value=-99999.0; // END OF SECTION THAT YOU MUST WRITE All of the code you will write must be placed between these START and END comments. Step 5: Write and test your code. Ok, easier said than done, right? Some hints: 1. Make sure you know how rectangle works. This will help you with trapezoid. 2. Compile and run your code obsessively while you are writing it!!! You should try to compile after each line (or each loop) of code you write. This will make locating your problem much easier because you will be able to pinpoint where the problem is. 3. Important. First, use your mechanical solutions from part 4 to test this code. If you use n=4, your code should produce the same thing that your paper and pencil did. 4. If you're having problems getting the numbers right, it's useful to temporarily add print statements to your code. Then, for each part of the problem, you can compare what prints on the screen to what your hand calculation shows. Step 6: Confirm that your code works. Here is the output from my code for various values of n [you must get the EXACT SAME]. a.exe 2 You have chosen to break the function into 2 intervals Rectangle Rule (n=2): Answer: 0.07497 (Error 92.0036%) Trapezoid Rule (n=2): Answer: 2.07497 (Error 121.3298%) a.exe 10 You have chosen to break the function into 10 intervals Rectangle Rule (n=10): Answer: 0.59664 (Error 36.3586%) Trapezoid Rule (n=10): Answer: 0.99664 (Error 6.3081%) a.exe 1000 You have chosen to break the function into 1000 intervals Rectangle Rule (n=1000): Answer: 0.93351 (Error 0.4260%) Trapezoid Rule (n=1000): Answer: 0.93751 (Error 0.0006%) a.exe 10000 You have chosen to break the function into 10000 intervals Rectangle Rule (n=10000): Answer: 0.93710 (Error 0.0427%) Trapezoid Rule (n=10000): Answer: 0.93750 (Error 0.0000%) a.exe 1000000 You have chosen to break the function into 1000000 intervals Rectangle Rule (n=1000000): Answer: 0.93750 (Error 0.0004%) Trapezoid Rule (n=1000000): Answer: 0.93750 (Error 0.0000%) If you can reproduce these 5, your code should be good to go! If not, please don't bother turning this in to the TA until you get it working correctly. 5. Numerical. Ok, we've seen that n=4 is not enough intervals to provide a decent answer for this integral, so we'll have to let the computer do this one for us. Step 1: I have done most of the code writing for you. You will only need to write a little bit of code within the larger code that we provide. First, copy my starter code over to your directory The code is available from the course webpage (in the homework section) and is titled: StarterCode.ch Once it is copied over to your Linux directory, you should rename it to something else. Step 2: Compile and run the code. You should get the following output: You forgot to enter the number of intervals You got this message because I set this code up to accept an integer on the command line that tells the code to break the integral up into N intervals. Try to run the code again, entering the number 4 on the command line. For example: a.exe 4 Now, you should get the following output. You have chosen to break the function into 4 intervals Rectangle Rule (n=4): Answer: 0.28203 (Error 69.9163%) Trapezoid Rule (n=4): Answer: -99999.00000 (Error 10666660.0000%) Step 4: Write your own code within function trapezoid. You will see that I left the following comments within function trapezoid: // START OF SECTION THAT YOU MUST WRITE // (-99999 is just a dummy number for "value" until you replace it with something meaningful) value=-99999.0; // END OF SECTION THAT YOU MUST WRITE All of the code you will write must be placed between these START and END comments. Step 5: Write and test your code. Ok, easier said than done, right? Some hints: 1. Make sure you know how rectangle works. This will help you with trapezoid. 2. Compile and run your code obsessively while you are writing it!!! You should try to compile after each line (or each loop) of code you write. This will make locating your problem much easier because you will be able to pinpoint where the problem is. 3. Important. First, use your mechanical solutions from part 4 to test this code. If you use n=4, your code should produce the same thing that your paper and pencil did. 4. If you're having problems getting the numbers right, it's useful to temporarily add print statements to your code. Then, for each part of the problem, you can compare what prints on the screen to what your hand calculation shows. Step 6: Confirm that your code works. Here is the output from my code for various values of n [you must get the EXACT SAME]. a.exe 2 You have chosen to break the function into 2 intervals Rectangle Rule (n=2): Answer: 0.07497 (Error 92.0036%) Trapezoid Rule (n=2): Answer: 2.07497 (Error 121.3298%) a.exe 10 You have chosen to break the function into 10 intervals Rectangle Rule (n=10): Answer: 0.59664 (Error 36.3586%) Trapezoid Rule (n=10): Answer: 0.99664 (Error 6.3081%) a.exe 1000 You have chosen to break the function into 1000 intervals Rectangle Rule (n=1000): Answer: 0.93351 (Error 0.4260%) Trapezoid Rule (n=1000): Answer: 0.93751 (Error 0.0006%) a.exe 10000 You have chosen to break the function into 10000 intervals Rectangle Rule (n=10000): Answer: 0.93710 (Error 0.0427%) Trapezoid Rule (n=10000): Answer: 0.93750 (Error 0.0000%) a.exe 1000000 You have chosen to break the function into 1000000 intervals Rectangle Rule (n=1000000): Answer: 0.93750 (Error 0.0004%) Trapezoid Rule (n=1000000): Answer: 0.93750 (Error 0.0000%) If you can reproduce these 5, your code should be good to go! If not, please don't bother turning this in to the TA until you get it working correctly
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started