How can I subtract this matrix in this function?

4 Ansichten (letzte 30 Tage)
Tyler Clarke
Tyler Clarke am 19 Mär. 2016
Kommentiert: Tyler Clarke am 20 Mär. 2016
Hello, I'm having some difficulty with implementing a matrix into a particular function I'm using in the code below:
% function dy = Verhulst_KT(t,y)
dy = zeros(1,1);
dy(1) = r*y(:)*(1-y(1)/(300 + 100*sin(pi/6*t-pi/2))-H );
end
Where H is defined by:
for i = 1:61
s = ones(1,60);
s(i) = rem(T(i),mod);
if s(i) < 10 && s(i) > 3
H(i) = 160;
else
H(i) = 0;
end
end
I've tried referring to individual positions in H within the function and to the whole matrix itself but either a single element is read which is not what I'm after or I get the error "In an assignment A(:) = B, the number of elements in A and B must be the same." I was just hoping I could get some quick assistance in what is hopefully a fairly simple problem. Thank you in advance.
EDIT: I'm using ode45 to solve the equation itself, T is a row matrix from 0:60 and r is a constant. In hindsight, I probably could've done with pasting in the whole code but I wanted to avoid unnecessary content, apologies for any confusion caused.
  2 Kommentare
Stephen23
Stephen23 am 19 Mär. 2016
What is T ? What values are you calling Verhulst_KT with ?
Jan
Jan am 19 Mär. 2016
A simpler method to obtain H:
S = rem(T, m); % Do not use "mod" as a name of a variable
H = and(S > 3, S < 10) * 160;

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John BG
John BG am 20 Mär. 2016
Tyler
1.- in order to have your V_KT function working, to prevent the error A(I)=B you mention and others i had to change it to the following:
function dy = V_KT(t,y) % your Verhulst_KT()
dy = zeros(1,1);
r=ones(1,length(y))
% H noise whatever not deflined
% dy(1) = r*y(:)*(1-y(1)./(300 + 100*sin(pi/6*t-pi/2))-H );
dy = r*y(:)*(1-y(1)./(300 + 100*sin(pi/6*t-pi/2)));
end
2.- regarding H, what are you trying to do with it, embed data or noise in the phase?
H has to have same size as t, your T otherwise when attempting to combine them inside the sin(pi/6 ..) MATLAB will say things referring to size mismatch.
3.-
you need to pass H in the function dy=V_KT(t,y,H)
otherwise how is dy going to have anything to do with H?
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John
  1 Kommentar
Tyler Clarke
Tyler Clarke am 20 Mär. 2016
I added H as a variable for the function as suggested in part 3 and redefined H in an if statement within the function also therefore making it a variable rather than a matrix and it turned out perfect. Thanks for your assistance.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by