이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
integral of two added function can't be implemented
조회 수: 5 (최근 30일)
이전 댓글 표시
Faisal Al-Wazir
2022년 10월 28일
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])
답변 (1개)
Paul
2022년 10월 28일
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
Faisal Al-Wazir
2022년 10월 29일
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
2022년 10월 29일
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
2022년 10월 30일
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
2022년 10월 30일
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
2022년 10월 31일
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
2022년 10월 31일
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
2022년 10월 31일
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
2022년 10월 31일
Faisal Al-Wazir
2022년 10월 31일
편집: Faisal Al-Wazir
2022년 10월 31일
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
2022년 10월 31일
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
2022년 10월 31일
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
2022년 10월 31일
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
2022년 10월 31일
편집: Paul
2022년 10월 31일
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
2022년 11월 5일
편집: Faisal Al-Wazir
2022년 11월 5일
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
2022년 11월 5일
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
