You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
integral of two added function can't be implemented
1 view (last 30 days)
Show older comments
im trying to answer q5 here
%Q4 answer
clc
clear
u = @(t) double(t>=0);
h = @(t) exp(-t).*(u(t)-u(t-1));
h1= @(t) u(t)-2.*u(t-1)+u(t-2)
h1 = function_handle with value:
@(t)u(t)-2.*u(t-1)+u(t-2)
h2= @(t) h(t) + h1(t)
h2 = function_handle with value:
@(t)h(t)+h1(t)
fplot(h2,[-3,3])
grid on
ylim([-1 2])
% Q5 answer
clc
clear
t=@(t) t
t = function_handle with value:
@(t)t
u = @(t) double(t>=0);
h = @(t) exp(-t).*(u(t)-u(t-1));
h1= @(t) u(t)-2.*u(t-1)+u(t-2)
h1 = function_handle with value:
@(t)u(t)-2.*u(t-1)+u(t-2)
h2= @(t) h(t) + h1(t)
h2 = function_handle with value:
@(t)h(t)+h1(t)
i=integral(h2,-inf,t)
Error using integral
Limits of integration must be double or single scalars.
Limits of integration must be double or single scalars.
fplot(i,[-3,3])
grid on
ylim([-1 2])
Answers (1)
Paul
on 28 Oct 2022
Hi Faisal,
If the plot for the answer to Q4 is corret, then does the integration really need to start from -inf?
However, I suggest revisiting Q4, that solution does not look correct. Why would the output of the system be the sum of the impulse response and the input?
16 Comments
Faisal Al-Wazir
on 29 Oct 2022
ok i think i didn't understood the qustion
i did some changes to q4
can you tell me if it's correct now ?
clc
clear
u = @(t) double(t>=0);
x= @(t) u(t)-2.*u(t-1)+u(t-2)
x = function_handle with value:
@(t)u(t)-2.*u(t-1)+u(t-2)
h1 = @(t) exp(-t).*(x(t)-x(t-1));
%h2= @(t) h(t) + h1(t)
fplot(h1,[-5,5])
grid on
ylim([-1 2])
Paul
on 29 Oct 2022
If h1(t) is supposed to be the answer to Q4, then no, it's not correct.
Also, the expression for h in the Question isn't correct. The u(t-1) should be u(t-2).
How are you trying to solve this problem?
Faisal Al-Wazir
on 30 Oct 2022
yes my bad so if i change u(t-1) to u(t-2) well it be right ?
if no, then what can i do ?
clc
clear
u = @(t) double(t>=0);
h = @(t) exp(-t).*(u(t)-u(t-1));
x= @(t) u(t)-2.*u(t-1)+u(t-2)
x = function_handle with value:
@(t)u(t)-2.*u(t-1)+u(t-2)
h1 = @(t) exp(-t).*(x(t)-x(t-2));
y1=@(t) (u(t)-2.*u(t-1)+u(t-2)).* (exp(-t).*(u(t)-u(t-1))) %maybe this one is right
y1 = function_handle with value:
@(t)(u(t)-2.*u(t-1)+u(t-2)).*(exp(-t).*(u(t)-u(t-1)))
fplot(h1,[-3,3])
grid on
ylim([-1 2])
Paul
on 30 Oct 2022
No, just changing the u(t-1) to u(t-2) in the definition of h will not be enough. Typical approaches to solving this problem would be to use convolution or the Laplace transform.
Faisal Al-Wazir
on 31 Oct 2022
thanks paul but when i use conv function it gives an empty plot
clc
clear
u = @(t) double(t>=0);
h = @(t) exp(-t).*(u(t)-u(t-1));
x= @(t) u(t)-2.*u(t-1)+u(t-2)
x = function_handle with value:
@(t)u(t)-2.*u(t-1)+u(t-2)
h1= @(t) conv(h,x)
h1 = function_handle with value:
@(t)conv(h,x)
fplot(h1,[-3,3])
grid on
ylim([-1 2])
Warning: Error updating FunctionLine.
The following error was reported evaluating the function in FunctionLine update: Input arguments to function include colon operator. To input the colon character, use ':' instead.
The following error was reported evaluating the function in FunctionLine update: Input arguments to function include colon operator. To input the colon character, use ':' instead.
Paul
on 31 Oct 2022
Recheck the doc page for conv. The inputs are vectors of numbers, not anonymous functions as you've tried above. So you have to evaluate the functions at the values of t of interest. Also, conv computes the convolution sum, it needs to be scaled appropriately to approximate a convolution integral. Here's an example.
u = @(t) double(t >= 0); % unit step
h = @(t) exp(-2*t).*(u(t) - u(t-1)); % impulse response
x = @(t) t.*(u(t) - u(t-2)); % input
y = @(t,dt) conv(h(t),x(t))*dt; % output, scaled by dt to convert from sum to integral
tval = 0:.001:5;
dt = tval(2);
yval = y(tval,dt);
yval = yval(1:numel(tval)); % only retain as many points as in t
plot(tval,yval)
Faisal Al-Wazir
on 31 Oct 2022
thank you so much paul
but for q5 the same method doesn't work
%%
clc
clear
u = @(t) double(t >= 0); % unit step
h = @(t) exp(-2*t).*(u(t) - u(t-1)); % impulse response
x = @(t) t.*(u(t) - u(t-2)); % input
y = @(t,dt) conv(h(t),x(t))*dt; % output, scaled by dt to convert from sum to integral
tval = 0:.001:5;
dt = tval(2);
yval = y(tval,dt);
yval = yval(1:numel(tval)); % only retain as many points as in t
plot(tval,yval)
%%
%q5
y1=@(t) int(y,-inf,tval)
y1 = function_handle with value:
@(t)int(y,-inf,tval)
yval1 = y1(tval,dt);
Error using solution
Too many input arguments.
Too many input arguments.
yval1 = yval1(1:numel(tval));
plot(tval,yval1)
Paul
on 31 Oct 2022
Faisal Al-Wazir
on 31 Oct 2022
Edited: Faisal Al-Wazir
on 31 Oct 2022
well i tried doing what you said but with no luck
and i still don't know how to add the limits (-inf to t)
%%
clc
clear
u = @(t) double(t >= 0); % unit step
h = @(t) exp(-2*t).*(u(t) - u(t-1)); % impulse response
x = @(t) t.*(u(t) - u(t-2)); % input
y = @(t,dt) conv(h(t),x(t))*dt; % output, scaled by dt to convert from sum to integral
tval = 0:.001:5;
dt = tval(2);
yval = y(tval,dt);
yval = yval(1:numel(tval)); % only retain as many points as in t
plot(tval,yval)
%%
%q5
y1= @(t) cumtrapz(y)
y1 = function_handle with value:
@(t)cumtrapz(y)
yval1 = y1(tval,dt);
Error using solution
Too many input arguments.
Too many input arguments.
yval1 = yval1(1:numel(tval));
plot(tval,yval1)
Paul
on 31 Oct 2022
No need to use a function handle. For example, suppose I want to integrate y(tau)*dtau from 0 to t, with y(t) equal to t^2. We know that the result should be t^3/3. Let's try it
tval = 0:.001:3;
yval = tval.^2;
plot(tval,cumtrapz(tval,yval))
Compare to closed form expression
plot(tval,tval.^3/3)
Faisal Al-Wazir
on 31 Oct 2022
so the final answer is this right ?
but the limits are not -inf to t
%%
clc
clear
u = @(t) double(t >= 0); % unit step
h = @(t) exp(-2*t).*(u(t) - u(t-1)); % impulse response
x = @(t) t.*(u(t) - u(t-2)); % input
y = @(t,dt) conv(h(t),x(t))*dt; % output, scaled by dt to convert from sum to integral
tval = 0:.001:5;
dt = tval(2);
yval = y(tval,dt);
yval = yval(1:numel(tval)); % only retain as many points as in t
plot(tval,yval)
%%
%q5
tval = 0:.001:3;
yval = conv(h(tval),x(tval))*dt;
yval = yval(1:numel(tval))
yval = 1×3001
1.0e+00 *
0 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0001 0.0001 0.0001 0.0001 0.0001 0.0002 0.0002 0.0002 0.0002 0.0002 0.0002 0.0003 0.0003 0.0003 0.0003 0.0004 0.0004 0.0004
plot(tval,cumtrapz(tval,yval))
Paul
on 31 Oct 2022
Looks ok for this example, but you'll need to adapt it to the actual problem. Not sure why you recomputed tval and yval for Q5 from Q4, but it doesn't matter I suppose.
"but the limits are not -inf to t"
Yes, the effective limits are 0 to t when using cumtrapz for this problem.
From first principles, what is y(t) for t < 0? You should be able to answer this question based only on the form of h(t) and x(t). If you know the answer to this question, then you'll know the answer about the limits of integration.
Paul
on 31 Oct 2022
Edited: Paul
on 31 Oct 2022
The impulse response is 0 for t < 0. The input to the system is 0 for t < 0. Consequently, would you expect the output of the system to be non-zero for any t < 0? If so, why? If not, then does it matter if the lower limit of integration on y(t) is -inf or 0?
Faisal Al-Wazir
on 5 Nov 2022
Edited: Faisal Al-Wazir
on 5 Nov 2022
greetings paul
i reviewed the code that we were discussing and i think the values you were using are differnt then mine
%%
%q4
clc
clear
u = @(t) double(t >= 0); % unit step
h = @(t) exp(-t).*(u(t) - u(t-2)); % impulse response
x = @(t) u(t-1)-2*u(t-2)+u(t-3); % input
y = @(t,dt) conv(h(t),x(t))*dt; % output, scaled by dt to convert from sum to integral
tval = 0:.001:5;
dt = tval(2);
yval = y(tval,dt);
yval = yval(1:numel(tval)); % only retain as many points as in t
plot(tval,yval)
%%
%q5
tval = 0:.001:3;
yval = conv(h(tval),x(tval))*dt;
yval = yval(1:numel(tval));
plot(tval,cumtrapz(tval,yval))
Paul
on 5 Nov 2022
Yes, as I stated previously, I was showing an example that would have to be modified for the specific problem at hand.
Your current code changes the definition of x(t) from the original Q4. For Q5, consider extending tval to a longer time.
See Also
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)