Error in function ?
显示 更早的评论
when I run it I got the following error
Undefined function or variable 'TBack'.
Error in interp (line 18)
TBack;
Error in physio (line 10)
v=interp(t);
Error in testingfile (line 47)
p1=physio(t(i)+alfa);
采纳的回答
I think that the statement
TBack;
at the end of the interp function does not belong. Just delete it.
Here is another issue:
In your interp function, you cannot execute the loop for k=1 to length (t_test) because you are referencing element (k+1) this will cause you to have an "out of bounds" subscript error when k=length(t_test).
The loop should run for k=1 to length(t_test)-1
7 个评论
Output argument "TBack" (and maybe others) not assigned
during call to "interp".
Error in physio (line 10)
v=interp(t);
Error in testingfile (line 47)
p1=physio(t(i)+alfa);
After changing them I still have the error
I think that the error with "TBack" is that the function output needs to be defined for all logic paths in the code.
You have one logic path (the first "if" condition) where this variable will not be assigned. You have two choices to fix this:
1) Add a definition for TBack in your first "if" statement, e.g.
if t1 <=0 || t1 >9
TBack = -99;
...
end
elsif
...
This represents a "nonsense" condition where the function is undefined.
2) Or you can assign a default value for TBack before the if statement.
This way, TBack will always have a value assigned.
function [TBack]=interp(t1);
t_test=[0 1 2 4 5 7 8 9];
T = [0 5 8 -6 -6 6 3 -4];
% find the t loction
for k=1:length(t_test)-1
if t1 <= 0 || t1 > 9
fprintf('Undefined \n')
TBack=-1;
% elseif t1 == 9
% TBack= -4;
break
elseif t_test(k) <= t1 && t_test(k+1)> t1
TBack=(((T(k+1)-T(k))/(t_test(k+1)-t_test(k)))*(t1-t_test(k)))+T(k);
end
end
%TBack;
end
it still has the error
I think it's going well when I change the for loop to 7 , but the result is completely wrong
%% The main
t=[1e-23 1 2 4 5 7 8 9];
x=1;
alfa=0.1;
h=0;
for i=1:1:7
p1=physio(t(i)+alfa);
p2 = physio(t(i));
h(i)=alfa*(p1- p2)
x(i+1)=x(i)*(1+0.8*h(i)*(1-(x(i)/25)))
end
plot(t,x)
hold on
title('The Locistic Model of a Population System Driven by physiological time')
xlabel('Time ,t')
ylabel('Population Size')
%% ode 45
tspan = [0 9];
x0 = 1;
[t,y] = ode45(@(t,x) 0.8*x*(1-(1/25)*x), tspan, x0);
plot(t,y)
grid on
title('The Locistic Model of a Population System Driven by Chronological time')
xlabel('Time ,t')
ylabel('Population Size')
If you are intending to interpolate T using t_test , then the correct formula is:
TBack = (t1-t_test(k))/(t_test(k+1)-t_test(k)) * (T(k+1)-T(k)) + T(k);

I just noticed that you commented out the condition
elseif t1 == 9
If you do this, then you should change the conditional on the next statement from
elseif t_test(k) <= t1 && t_test(k+1) > t1
to
elseif t_test(k) <= t1 && t_test(k+1) >= t1
otherwise, there is no defined action for t1 = 9.
I'm afraid that I cannot help you if you do not like the results, since I do not understand the problem you are working.
But if I have been helpful, please accept my answer.
Perhaps you should clean up your code, and then post a new question showing the code and the results you are getting, and why you think it is not right (i.e. what is the expected result?). Maybe someone else knows about this particular type of problem.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
另请参阅
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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)
