I nee to write a code to solve a definite integral. The output format has to be fprintf.

1 view (last 30 days)
Aj on 13 May 2022
Commented: Voss on 13 May 2022
%0 to 3 ∫(−x^3 + 3x^2 − 2) dx
syms x
f = -x.^3 + 3.x^2 - 2;
a=0;
b=3;
int(f, a, b);
This is the code i have so far but i keep getting an error. If some one can help me please and thank you. Formatted output must be frprintf.

Voss on 13 May 2022
Edited: Voss on 13 May 2022
syms x
% f = -x.^3 + 3.x^2 - 2;
% ^ this . should be *
f = -x.^3 + 3*x^2 - 2;
a=0;
b=3;
output = int(f, a, b)
output =
fprintf('%f',output)
0.750000
Voss on 13 May 2022
You're welcome!

John D'Errico on 13 May 2022
Edited: John D'Errico on 13 May 2022
Do you understand that MATLAB REQUIRES a multiplication operator? You cannot just put a number next to a letter, and expect MATLAB to know this is multiplication. So this is not valid code. Did you read the error message? It tells you that.
f = -x.^3 + 3.x^2 - 2;
Fixing that problem, we have
syms x
f = -x.^3 + 3*x^2 - 2;
a=0;
b=3;
Next, you need to assign the result of the integration to something.
res = int(f, a, b)
res =
You now need to turn that symbolic variable into a double.
Dres = double(res)
Dres = 0.7500
Now use fprintf. Read the help for fprintf.