필터 지우기
필터 지우기

error Conversion to double from function_handle is not possible.

조회 수: 1 (최근 30일)
Ivan
Ivan 2023년 10월 24일
댓글: Star Strider 2023년 10월 25일
t = 0:0.01:10;
nt = length(t);
% Inicialización de u y u'
u = zeros(size(t));
p = zeros(1,nt);
for i = 1:nt
p(i) = @(t) (10./(t(i)+1));
u(i) = integral(@(tau) p(t(i)-tau)*h(tau),0,t(i));
end
me aparece el error Conversion to double from function_handle is not possible.
p(i) = @(t) (10./(t(i)+1));
como solucionarlo?

채택된 답변

Star Strider
Star Strider 2023년 10월 24일
The ‘p’ function was incorrect, and the ‘h’ funciton is completely missing (so I created it).
Try this (with the correct ‘h’ function) —
t = 0:0.01:10;
nt = length(t);
% Inicialización de u y u'
u = zeros(size(t));
p = zeros(1,nt);
p = @(t) (10./(t+1));
h = @(x) x; % Create Function
for i = 1:nt
u(i) = integral(@(tau) p(t(i)-tau).*h(tau),0,t(i));
end
u
u = 1×1001
0 0.0005 0.0020 0.0045 0.0079 0.0123 0.0177 0.0239 0.0312 0.0393 0.0484 0.0584 0.0693 0.0811 0.0937 0.1073 0.1217 0.1369 0.1531 0.1700 0.1879 0.2065 0.2260 0.2463 0.2674 0.2893 0.3120 0.3355 0.3598 0.3849
.
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 10월 24일
p = zeros(1,nt);
p = @(t) (10./(t+1));
Why bother to initialize p with zeros there?
Star Strider
Star Strider 2023년 10월 25일
It was initially written as:
p(i) = @(t) (10./(t(i)+1));
and I didn’t catch the preallocation when I corrected the ‘p’ function. (I was concentrating on it and the absent ‘h’ function.)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 10월 24일
Here is the corrected solution:
t = 0:0.01:10;
nt = length(t);
% Inicialización de u y u'
u = zeros(size(t));
p = zeros(1,nt);
p = @(t) (10./(t+1));
Pval=p(t);
% Note that your include h() is unknown. Thus it is removed from the
% formulation. Predefine it if it is to be included.
for i = 1:nt
u(i) = integral(@(tau) Pval(i)*(t(i)-tau).*(tau),0,t(i));
end

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by