How can i solve the Too many output arguments error problem ?

조회 수: 3 (최근 30일)
Hazel Can
Hazel Can 2023년 1월 2일
댓글: Star Strider 2023년 1월 2일
clc
clear
syms x
f=input('\n Enter the function f(x): '); %inline('((x^2)*log(x))')
f=@ (x) (x^2)*log(x)
a=input('Enter lower limit a: '); % for our example a=1
b=input('Enter upper limit b: '); % for our example b=5
n=input('Enter the number of sub-intervals n: '); % for our example n=8,16,32
h=(b-a)/n;
Df=diff((x.^2)*log(x),x,4);
if rem(n,2)==1
fprintf('\n Enter valid n!!!');
n=input('\n Enter n as even number ');
end
so=0;
se=0;
for k=1:1:n-1
x(k)=a+k*h;
y(k)=f(x(k));
if rem(k,2)==1
so=so+y(k);%sum of odd terms
else
se=se+y(k); %sum of even terms
end
end
% Formula: (h/3)*[(y0+yn)+4*(y1+y3+y5+..odd term)+2*(y2+y4+y6+...even terms)]
answer=h/3*(f(a)+f(b)+4*so+2*se);
Error=-(((b-a)/180)*(h^4)*(Df))
fprintf('\n The value of integration is %f',answer);
fprintf('\n The value of error is %f',error);
and after the run, error is :
Error using error
Too many output arguments.
Error in compos (line 30)
fprintf('\n The value of error is %f',error);

답변 (1개)

Star Strider
Star Strider 2023년 1월 2일
You are overshadowing the error function (although there are also two other error functions in the Statistics and Machine Learning Toolbox that do have outputs).
fprintf('\n The value of error is %f',error);
Error using error
Too many output arguments.
In any event, it should probably be:
Error=-(((b-a)/180)*(h^4)*(Df))
fprintf('\n The value of error is %f',Error);
instead.
.
  댓글 수: 2
Torsten
Torsten 2023년 1월 2일
... and since "Error" is symbolic, it cannot be printed the way you do.
Star Strider
Star Strider 2023년 1월 2일
@Torsten — Noted! I missed the syms call.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by