Integrating f(x) with Trapezium Rule & Plotting for Different Values of C

In summary, the conversation is about writing a script file to integrate a function using the trapezium rule and calculating the integral for different values of C. The code provided is able to calculate the integrals, but not plot them. Modifications are suggested to add an outer loop and change the variable 'area' to an array in order to plot the integrals for different values of C on the same graph.
  • #1
Rachelross
4
0

Homework Statement


Write a script file to integrate the function f(x)=x^3+tan(x)+C using the trapezium rule.Calculate the integral between the end points x=2 and x=4 with strip size 0.1.Calculate the integral for range of values from 1 to 3 in steps of 0.1.plot the integral obtained for different values of C?


Homework Equations


I wrote the code and it is calculating the correct integrals but it is not plotting the integrals obtained for different values of C .can u please help me to modify my code so that it plots :)



The Attempt at a Solution


This is my code that I have been working on:

% What are the integration limits?
clc
a = 2;
b =4;
% What is the integration step size?
dx = 0.1;
% An array containing the x points.
x = [a:dx:b];
% What is the function to integrate?
C=1:0.1:3;
C;
y =x.^3+tan(x)+C;
% The number of points
N = length(y);
%initialise the area variable
sum_y = 0;
%sum the interior elements of the y array
%note how the loop starts at 2 and stops at N-1
for I = 2:N-1
sum_y = sum_y + y(I);
area = dx*((y(1) + y(N))/2 + sum_y)

plot(area,C)
end

Thank you for your help:)
 
Physics news on Phys.org
  • #2
Hi it seems to me that you have find area for different values of C. For every value of C, you have to integrate and find area, this needs outer 'for' loop. To plot the result the variable 'area' should be an array. I have modified your program slightly as given below.

clc
a = 2;
b =4;
% What is the integration step size?
dx = 0.1;
% An array containing the x points.
x = [a:dx:b];
% What is the function to integrate?
for C=1:0.1:3; % This loop to find area for different values of C
y =x.^3+tan(x)+C;
% The number of points
N = length(y);
%initialise the area variable
sum_y = 0;
%sum the interior elements of the y array
%note how the loop starts at 2 and stops at N-1
i=1; % index to store area in an array
for I = 2:N-1
sum_y = sum_y + y(I);
area (i)= dx*((y(1) + y(N))/2 + sum_y); % variable area is an array
i=i+1;
end
plot(area);
hold on % To plot areas for different values of C on same graph
end

Hope this helps
 
  • #3
Thank you so much n.karthick :)
 

1. What is the Trapezium Rule and how is it used to integrate f(x)?

The Trapezium Rule is a numerical method used to approximate the value of a definite integral. It involves dividing the area under a curve into small trapezoidal shapes and summing their areas to get an estimate of the integral. This method is useful for functions that cannot be easily integrated using traditional methods.

2. How do I use the Trapezium Rule to integrate f(x) for a specific range of values?

To use the Trapezium Rule, you first need to divide the range of values into equally spaced intervals. Then, calculate the value of f(x) at each interval and use those values to calculate the areas of the trapezoids. Finally, sum the areas to get an estimate of the integral for that range of values.

3. Can the Trapezium Rule be used for any type of function f(x)?

Yes, the Trapezium Rule can be used for any type of function f(x) as long as it is continuous within the given range of values. However, for functions with sharp curves or discontinuities, the accuracy of the estimate may be affected.

4. How do I plot the results of integrating f(x) using the Trapezium Rule for different values of C?

To plot the results, you can create a table with the different values of C and their corresponding estimates of the integral. Then, use a graphing software to plot the points and connect them with a smooth curve. This will give you a visual representation of how the integral changes with different values of C.

5. Is the Trapezium Rule more accurate than other numerical integration methods?

The Trapezium Rule is not necessarily more accurate than other methods, such as Simpson's rule or the midpoint rule. The accuracy of the estimate depends on the function being integrated and the number of intervals used. It is recommended to compare the results from different methods to determine the most accurate estimate.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
882
  • Engineering and Comp Sci Homework Help
Replies
1
Views
831
  • Engineering and Comp Sci Homework Help
Replies
6
Views
859
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
955
  • Engineering and Comp Sci Homework Help
Replies
7
Views
888
Back
Top