Error Undefined x!

1 次查看(过去 30 天)
Zhuoying Lin
Zhuoying Lin 2017-11-10
评论: Star Strider 2017-11-12
In my script, I call a function value to input values and put the input in another function integrnfn:
[A,x1,x2]=value;
y=integrfn(A,x1,x2);
fprintf('The value of the integral is %.3f', integrfn(A,x1,x2))
function [A,x1,x2]=value
A=input('Please enter a positive value: ');
x1=input('Please enter another positive value: ');
x2=input('Please enter a positive value that is greater than the previous one: ');
end
function y=integrfn(A,x1,x2)
y=int(A*exp(-x^2)*cos(2*x)/(1+x^2),x,x1,x2);
end
However, when I run it, it says:
Undefined function or variable 'x'.
Error in script >integrfn (line 15)
y=int(A*exp(-x^2)*cos(2*x)/(1+x^2),x,x1,x2);
Error in the script (line 5)
y=integrfn(A,x1,x2);
Can someone tell me the problem?
Thank you!!

采纳的回答

Star Strider
Star Strider 2017-11-10
The int function is part of the Symbolic Math Toolbox. The most appropriate function is integral, with the integrand as an anonymous function:
y = integral(@(x) A*exp(-x.^2).*cos(2*x)./(1+x.^2),x1,x2);
  6 个评论
Zhuoying Lin
Zhuoying Lin 2017-11-12
I figure out why now. Thank you!
Star Strider
Star Strider 2017-11-12
My pleasure!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by