Complete the code to calculate the integral of sin(x) from 0 to pi using the integral function.
result = integral(@(x) sin(x), 0, [1]);
The integral of sin(x) from 0 to pi is calculated by setting the upper limit to pi.
Complete the code to approximate the integral of cos(x) from 0 to pi using the trapz function with 100 points.
x = linspace(0, pi, 100); y = cos(x); result = trapz(x, [1]);
The trapz function integrates the values in y over x. So we pass y as the second argument.
Fix the error in the code to correctly compute the integral of exp(-x^2) from -1 to 1 using integral.
result = integral(@(x) exp(-[1].^2), -1, 1);
The anonymous function uses x as the input variable, so the variable inside the function must be x.
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.
x = 1:5; values = x(x [2] 4).[1]2;
Use .^ for element-wise power and > to filter values greater than 4.
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.
result = struct(); for i = 1:length(x) if y(i) [1] 0.5 field = upper([2](i)); result.(field) = [3](i); end end
The condition checks if y(i) is greater than 0.5, the field name is uppercase of x(i), and the value assigned is y(i).