Error in function ?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
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 件のコメント
Willim
2019 年 2 月 10 日
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
Willim
2019 年 2 月 10 日
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 についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
