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

6 ビュー (過去 30 日間)
Aj
Aj 2022 年 5 月 13 日
コメント済み: Walter Roberson 2022 年 10 月 7 日
%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.

回答 (2 件)

Voss
Voss 2022 年 5 月 13 日
編集済み: Voss 2022 年 5 月 13 日
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 件のコメント
Jomar
Jomar 2022 年 10 月 7 日
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.
Walter Roberson
Walter Roberson 2022 年 10 月 7 日
What error message are you observing? Which MATLAB version are you using?

サインインしてコメントする。


John D'Errico
John D'Errico 2022 年 5 月 13 日
編集済み: John D'Errico 2022 年 5 月 13 日
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.

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by