How can I define initial condition in a for loop?
Show older comments
Hi, I want to compute activity vectors vi from an initial activity v0 = [ 9 9] . I want to do it for time 0 to time 10 in steps of ∆t = 0.0001. At each iteration step, I have to compute vi+1 from vi as follows: vi+1 = vi + F (A · v + b) ∆t. The F is given as a function.
This is what I have done so far:
v_0= [9; 9];
T=10;
dt=0.0001;
v1=zeros(10000,2);
v=zeros(10000,2);
v1=v_0+Factivation(X, Rmax, k)*dt;
for i=1:10000*T
v(i+1,:)=v1(i,:)+Factivation(X, Rmax, k)*dt;
end
It doesn't seem correct. I do not know how to incoorporate the initial condition v(0) correctly and also I get this error:
Array indices must be positive integers or logical values.
Could you please help me?
Answers (2)
v_0= [9; 9];
T=10;
dt=0.0001;
% define the time step in the time vector
K = 1:dt:T
v1=zeros(length(K)+1,3);
v=zeros(length(K)+1,3);
% take a transpose of v_0
v1=v_0.'+Factivation(X, Rmax, k)*dt;
% use in the for loop
for I=1:length(K)
v(I+1,:)=v1(I,:)+Factivation(X, Rmax, k)*dt;
end
syms x y(x)
Dy = diff(y);
sol = dsolve(diff(y,x,2)+y==0,y(0)==9,Dy(0)==9)
v_0 = [9;9];
T = 10;
dt = 0.0001;
N = T/dt;
v = zeros(N+1,2);
t = (0:dt:T).';
v(1,:) = v_0;
f = @(t,y) [y(2),-y(1)]; % Replace "f" by your function "Factivation". It must return a row vector of length 2
for i = 1:N
v(i+1,:) = v(i,:) + dt*f(t(i),v(i,:));
end
hold on
plot(t,v)
plot(t,[9*(sin(t)+cos(t)),9*(cos(t)-sin(t))])
hold off
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!