integral of two added function can't be implemented
1 次查看(过去 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.
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numbers and Precision 的更多信息
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 (한국어)