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);

 Risposta accettata

Jim Riggs
Jim Riggs il 10 Feb 2019
Modificato: Jim Riggs il 10 Feb 2019

0 voti

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 Commenti

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
Jim Riggs
Jim Riggs il 10 Feb 2019
Modificato: Jim Riggs il 10 Feb 2019
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.
Willim
Willim il 10 Feb 2019
Modificato: Willim il 10 Feb 2019
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')
Jim Riggs
Jim Riggs il 10 Feb 2019
Modificato: Jim Riggs il 10 Feb 2019
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);
MatlabAnswers 20190210.JPG
Jim Riggs
Jim Riggs il 10 Feb 2019
Modificato: Jim Riggs il 10 Feb 2019
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.
Jim Riggs
Jim Riggs il 11 Feb 2019
Modificato: Jim Riggs il 11 Feb 2019
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 10 Feb 2019

Modificato:

il 11 Feb 2019

Community Treasure Hunt

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

Start Hunting!

Translated by