Identify the error in this MATLAB code snippet that tries to compute the square root of each element in vector A:
A = [4, 9, 16, 25];
B = sqrt A;
medium
A. sqrt function requires two arguments
B. Using square root on a vector is not allowed
C. Vector A should be a column vector
D. Missing parentheses around A in sqrt function
Solution
Step 1: Check MATLAB function syntax
Functions require parentheses around their arguments, so sqrt A is invalid syntax.
Step 2: Correct the syntax
Use sqrt(A) to compute element-wise square roots of vector A.
Final Answer:
Missing parentheses around A in sqrt function -> Option D
Quick Check:
Function calls need parentheses [OK]
Hint: Always use parentheses for function arguments in MATLAB [OK]
Common Mistakes:
Omitting parentheses in function calls
Thinking sqrt can't handle vectors
Assuming sqrt needs multiple arguments
5.
You want to approximate the integral of f(x) = exp(-x^2) from 0 to 1 using numerical computation in MATLAB. Which approach best solves this real problem?
hard
A. Manually calculate the exact integral using symbolic math
B. Use integral(@(x) exp(-x.^2), 0, 1) to compute the numerical integral
C. Use a for loop to sum exp(-x^2) at random points without spacing
D. Approximate by taking the value at x=0.5 only
Solution
Step 1: Understand the integral problem
The integral of exp(-x^2) has no simple exact formula, so numerical methods are needed.
Step 2: Use MATLAB's numerical integration function
The integral function with an anonymous function handles this efficiently and accurately.
Final Answer:
Use integral(@(x) exp(-x.^2), 0, 1) to compute the numerical integral -> Option B
Quick Check:
Numerical integration with integral() solves complex integrals [OK]
Hint: Use integral() with function handle for numerical integration [OK]
Common Mistakes:
Trying to find exact integral manually
Summing values at random points without spacing
Using a single point value as approximation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.