Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

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.c" 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 Nintervals. 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%) Side note: as you can see from the code, to print a % sign, you need to use %%. This is because "%" is used in C as a format specifier, so to print a %, you need to type "%%". Note, interestingly, I've found that this is needed on all compilers, but to be safe, recommend always typing %% when you want to print a %. Step 3: So, what's up with this code? I wrote this code to integrate the function used in this homework using both the rectangle rule and the trapezoid rule. The program includes 4 functions (these are program functions, not mathematical functions): 1. main - this is the main program. 2. rectangle - this function computes the integral of the function using the rectangle rule. 3. trapezoid - this function computes (or it will when you are finished) the integral of the function using the trapezoid rule. 4. function - this function contains the mathematical function in this homework problem. I conveniently deleted most of the code within the function trapezoid. This is all you will have to fill in - write the proper code into the trapezoid function. First thing you will want to do, is to read this code and to mentally reverse-engineer it to the best of your ability. I put lots of comments in there to help. You should have a semi-ok understanding as to how this code works before writing your piece to it. But don't worry if you don't fully understand it because it includes a lot of programming stuff we haven't got to yet. Next, you will want to extra-carefully understand the function rectangle. I wrote rectangle to provide you with a guide as to how to do this type of thing. rectangle provides you with a template for writing your code within function trapezoid. 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.c" 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 Nintervals. 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%) Side note: as you can see from the code, to print a % sign, you need to use %%. This is because "%" is used in C as a format specifier, so to print a %, you need to type "%%". Note, interestingly, I've found that this is needed on all compilers, but to be safe, recommend always typing %% when you want to print a %. Step 3: So, what's up with this code? I wrote this code to integrate the function used in this homework using both the rectangle rule and the trapezoid rule. The program includes 4 functions (these are program functions, not mathematical functions): 1. main - this is the main program. 2. rectangle - this function computes the integral of the function using the rectangle rule. 3. trapezoid - this function computes (or it will when you are finished) the integral of the function using the trapezoid rule. 4. function - this function contains the mathematical function in this homework problem. I conveniently deleted most of the code within the function trapezoid. This is all you will have to fill in - write the proper code into the trapezoid function. First thing you will want to do, is to read this code and to mentally reverse-engineer it to the best of your ability. I put lots of comments in there to help. You should have a semi-ok understanding as to how this code works before writing your piece to it. But don't worry if you don't fully understand it because it includes a lot of programming stuff we haven't got to yet. Next, you will want to extra-carefully understand the function rectangle. I wrote rectangle to provide you with a guide as to how to do this type of thing. rectangle provides you with a template for writing your code within function trapezoid. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Database Systems Design Implementation And Project Management

Authors: Albert K.W. Yeung, G. Brent Hall

1st Edition

1402053932, 978-1402053931

More Books

Students also viewed these Databases questions

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago