RK4 Method for Windkessel error
Show older comments
I have these errors
Attempted to access P(1.16667); index must be a positive integer or logical.
Error in @(t,P,Q)(-P(t)/(R*C))+(Q(t)/C)
Error in RK4_WINDKESSEL (line 24)
k2=f(t(i)+0.5*k1*h, P(i)+0.5*k1, Q(i));
clear all
close all
clc
%parameters
R=1.5; %resistance
C=4; %compliance
h=1; %step size
t=1:h:100; %time vector
P(1)=1; %initial conditions
Q(1)=1; %initial conditions
f=@(t,P,Q) (-P(t)/(R*C))+(Q(t)/C);
%RK4 LOOP
for i=1:ceil(100/h)
if (i>=1 || i<=5)
Q(i)=i+1; %ramp
end
Q(i+5)=Q(i); %ramp
t(i+1)=t(i)+h;
k1=f(t(i),P(i),Q(i));
k2=f(t(i)+0.5*k1*h, P(i)+0.5*k1, Q(i));
k3=f(t(i)+0.5*k2*h, P(i)+0.5*k2);
k4=f(t(i)+k3*h, P(i)+0.5*k3);
P(i+1)=P(i)+(1/h)*6*(k1+2*k2+2*k3+k4);
end
plot(t,P)
xlabel('time')
ylabel('Pressure')
Accepted Answer
More Answers (0)
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
