0
0
MATLABdata~30 mins

Numerical integration (integral, trapz) in MATLAB - Mini Project: Build & Apply

Choose your learning style9 modes available
Numerical integration using integral and trapz in MATLAB
📖 Scenario: You are working as a data analyst and need to calculate the area under a curve representing a physical measurement over time. This is a common task in science and engineering to find total quantities from rate data.
🎯 Goal: Build a MATLAB script that calculates the integral of a function using both the integral function and the trapz function for numerical integration. You will compare the results to understand how these methods work.
📋 What You'll Learn
Create a vector of x values from 0 to 10 with 100 points
Create a vector y with the function values y = sin(x)
Use the integral function to calculate the integral of sin(x) from 0 to 10
Use the trapz function to calculate the integral approximation from the x and y vectors
Print both results to compare
💡 Why This Matters
🌍 Real World
Numerical integration is used in physics, engineering, and data analysis to find total quantities like distance from speed or area under curves from measurements.
💼 Career
Understanding numerical integration helps in roles like data analyst, engineer, or scientist where you work with experimental data or simulations.
Progress0 / 4 steps
1
Create the x vector
Create a vector called x with 100 points linearly spaced from 0 to 10 using the linspace function.
MATLAB
Need a hint?

Use linspace(0, 10, 100) to create 100 points between 0 and 10.

2
Calculate y = sin(x)
Create a vector called y that contains the sine of each value in x using the sin function.
MATLAB
Need a hint?

Use y = sin(x); to get sine values for each x.

3
Calculate integrals using integral and trapz
Calculate the integral of sin(x) from 0 to 10 using the integral function and store it in int_val. Then calculate the integral approximation using trapz with x and y and store it in trapz_val.
MATLAB
Need a hint?

Use integral(@sin, 0, 10) and trapz(x, y) to calculate integrals.

4
Display the results
Print the values of int_val and trapz_val using fprintf to show the integral results with 6 decimal places.
MATLAB
Need a hint?

Use fprintf('Integral using integral: %.6f\n', int_val); and similarly for trapz_val.