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

5 views (last 30 days)
%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.

Answers (2)

Voss
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
  4 Comments
Jomar
Jomar on 7 Oct 2022
Sir can you help me to see what is wrong with my code
syms x
y = 0.5*x;
z = int(y, 0, 10);
fprintf('%.2f',z)
it keeps showing error.

Sign in to comment.


John D'Errico
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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!