Not enough input arguments.

1 view (last 30 days)
jiarong chen
jiarong chen on 19 Oct 2014
Edited: Mike Hosea on 22 Oct 2014
I am really new to use Matlab, can somebody help me?
Write a function in Matlab that generates the output y(t):
I did not study Matlab before, but I have to use Matlab to solve EE question. The following is my cord.
Please help me:
function u1=unit_step(x)
if x>=0
u1=1;
else
u1=0; end
function u2=unit_step(x)
if x>=t
u2=0;
else
u2=1; end
f = @(x) (x-t).*exp(x-t).*u1.*x^2.*exp(-x).*sin(x).*u2
Q = integral(f,-Inf,Inf)
---------------------Error using HW2_7 (line 2) Not enough input arguments.

Accepted Answer

Mike Hosea
Mike Hosea on 20 Oct 2014
Edited: Mike Hosea on 20 Oct 2014
I try not to do too many homework problems here because in a former life I was a college professor, and I think it's bad for the student just to hand them answers. But I think I should explain the error message.
When you have a function defined in a MATLAB file (ending in .m or .p), then when referring to the function, you must use @, e.g.
Q = integral(@f,-Inf,Inf)
The @ symbol tells MATLAB that you're referring the function, so it won't try to evaluate it immediately and pass the return values into the function.
If, OTOH, you have a function defined as an anonymous function e.g.
f = @(x)sin(x+2)
then you can refer to it by the name of the variable that is "holding" the anonymous function,
Q = integral(f,-Inf-Inf)
  2 Comments
jiarong chen
jiarong chen on 22 Oct 2014
Thank you for you answer. What professor requires us to solve this equation by hand and verifies the answer by using Matlab. I already solve the equation by hand, but I am just struggling on the verifying.
I put @ on that line,
f = (x-t).*exp(x-t).*u1.*x^2.*exp(-x).*sin(x).*u2
Q = integral(@f,-Inf,Inf)
,but there is still an error:
Error: File: HW2_7.m Line: 13 Column: 15
"f" was previously used as a variable, conflicting with its use here
as the name of a function or command.
Please tell me the how to the way how to solve it, and the reason for that.
Thank you so much
Mike Hosea
Mike Hosea on 22 Oct 2014
Edited: Mike Hosea on 22 Oct 2014
Sorry, I didn't read the problem correctly, so my information was not particularly helpful for the issue at hand. You had the syntax of the integral call correct to begin with. The problem is the way you're trying to do the unit step functions. Also, I think your next problem is that you want to use more than one value of t. The thing is, when you define a function using @(x) all other variables will have their "snapshots" taken at that moment. If t does not have a value, it will never have one! Check this out:
>> clear t
>> f = @(x)x + t
f =
@(x)x+t
>> f(1)
Undefined function or variable 't'.
Error in @(x)x+t
>> t = 1
t =
1
>> f(1)
Undefined function or variable 't'.
Error in @(x)x+t
>> f = @(x)x + t
f =
@(x)x+t
>> f(1)
ans =
2
>> t = pi
t =
3.141592653589793
>> f(1)
ans =
2
This happens because t is a variable and not an argument to the function. The system takes a snapshot of its value at that moment the function is created. If you want to change t, you will have to re-define the function. Depending on the context, you may find it less confusing to first create a function of two variables and then "bind" the other when you are ready to, e.g.
>> clear t
>> f = @(x,t)x + t
f =
@(x,t)x+t
>> t = 1;
>> integral(@(x)f(x,t),0,t)
ans =
1.500000000000000
>> t = 2;
>> integral(@(x)f(x,t),0,t)
ans =
5.999999999999999
Now, to fix your step functions, you can use Matt J's approach, but you can also just bring the Boolean expressions inline:
f = @(x,t)(x-t).*exp(x-t).*(x <= t).*x.^2.*exp(-x).*sin(x).*(x >= 0)
where I have made this a function of both x and t for now. Note that I changed x^2 to x.^2. Then do
integral(@(x)f(x,t),...)
This will draw in the current value of t when the integral function is called.
I would also recommend that you tell INTEGRAL about the fact that something interesting happens at x == 0 and at x == t using the Waypoints options:
integral(@(x)f(x,t),-inf,inf,'Waypoints',[0,t])
But you should think about whether you even need to integrate from -inf to inf. You won't need to tell the integrator about the Waypoints, or even need to include the Boolean expressions in the definition of the integrand, if you integrate from 0 to t instead.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 19 Oct 2014
Edited: Matt J on 19 Oct 2014
u=@(z) z>=0;
f = @(x) (x-t).*exp(x-t).*u(x).*x^2.*exp(-x).*sin(x).*u(t-x)
Q = integral(f,-Inf,Inf)

Community Treasure Hunt

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

Start Hunting!