I can't use int for functions

2 Ansichten (letzte 30 Tage)
f4r3in
f4r3in am 29 Jan. 2017
Bearbeitet: Star Strider am 29 Jan. 2017
I use Matlab R2016a. I write a function but I can't use int to get integrate:
function y=g1(x)
if x>0
y=1;
else
y=-1;
end
end
and when I run :" int(g1) " I receive this error :
int(g1)
Not enough input arguments.
Error in g1 (line 2)
if x>0
what should I do. please help step by step because I am beginner.

Antworten (1)

Star Strider
Star Strider am 29 Jan. 2017
Bearbeitet: Star Strider am 29 Jan. 2017
Your ‘g1’ is a function, so you have to pass it the argument you wrote it to evaluate. The integral function does this implicitly (so you do not actually see it).
I would use the integral function, not the Symbolic Math Toolbox int function (unless you have to do this symbolically):
g1 = @(x) (x>0).*(1) + (x<=0).*(-1);
a = -pi;
b = pi;
Result = integral(g1, a, b);
I used ‘logical indexing’ to define ‘g1’ here. It is more efficient.

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by