0
0
MATLABdata~10 mins

Numerical integration (integral, trapz) in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the integral of sin(x) from 0 to pi using the integral function.

MATLAB
result = integral(@(x) sin(x), 0, [1]);
Drag options to blanks, or click blank then click option'
Api
B2*pi
C0
Dpi/2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2*pi as the upper limit instead of pi.
Setting the upper limit to 0 which results in zero area.
2fill in blank
medium

Complete the code to approximate the integral of cos(x) from 0 to pi using the trapz function with 100 points.

MATLAB
x = linspace(0, pi, 100);
y = cos(x);
result = trapz(x, [1]);
Drag options to blanks, or click blank then click option'
Acos(x).^2
Bx
Cy
Dsin(x)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing x instead of y to trapz.
Passing an unrelated function like sin(x).
3fill in blank
hard

Fix the error in the code to correctly compute the integral of exp(-x^2) from -1 to 1 using integral.

MATLAB
result = integral(@(x) exp(-[1].^2), -1, 1);
Drag options to blanks, or click blank then click option'
AX
Bt
Cy
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase X instead of lowercase x.
Using a different variable name like t or y.
4fill in blank
hard

Fill both blanks to create a vector of squares for x from 1 to 5, including only values greater than 4, using logical indexing. This can be useful for selecting weights or points in numerical integration.

MATLAB
x = 1:5;
values = x(x [2] 4).[1]2;
Drag options to blanks, or click blank then click option'
A.^
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of .^ for element-wise power.
Using < instead of > in the condition.
5fill in blank
hard

Fill all three blanks to create a structure with fields as uppercase letters of x, values as y, and filter y values greater than 0.5. This can store selected significant function values for numerical integration points.

MATLAB
result = struct();
for i = 1:length(x)
    if y(i) [1] 0.5
        field = upper([2](i));
        result.(field) = [3](i);
    end
end
Drag options to blanks, or click blank then click option'
A>
Bx
Cy
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the condition.
Using y instead of x for the field name.
Assigning x(i) instead of y(i) as the value.