Undefined function 'rk' for input arguments of type 'function_handle'.
Show older comments
Hi guys, good day,
I have the following code to obtain the solution for an equation system, using Runge Kutta second order. But when I try to call the function, the "Undefined function 'rk' for input arguments of type 'function_handle'." message appears. What can I do? Please, help. Thanks
a=input('parámetro a: ');
b=input('parámetro b: ');
s0=input('valor inicial de s: ');
c0=input('valor inicial de c: ');
r0=input('valor inicial de r: ');
tf=input('tiempo final, tf: ');
n=input('número de pasos, n: ');
f=@(t,s,c,r) -a*s*c;
g=@(t,s,c,r) a*s*c-b*c;
j=@(t,s,c,r) b*c;
%condiciones iniciales
t0=0;
[t,s,c,r] =rk(f,g,j,t0,tf,s0,c0,r0,n);
hold on
plot(t,s,'b')
plot(t,c,'r')
plot(t,r,'g')
xlabel('t')
ylabel('s,c,r');
legend('s(t)','c(t)','r(t)')
title('ds/dt=-a*s*c, dc/dt=a*s*c-b*c, dr/dt=b*c')
hold off
function [t,s,c,r] =rk(f,g,j,t0,tf,s0,c0,r0,n)
h=(tf-t0)/n;
t=t0:h:tf;
s=zeros(n+1,1); %reserva memoria para n+1 element(i)os del vect(i)or x(i)
c=zeros(n+1,1);
r=zeros(n+1,1);
s(1)=s0; c(1)=c0; r(1)=r0;
for i=1:n
k1=h*f(t(i),s(i),c(i),r(i));
l1=h*g(t(i),s(i),c(i),r(i));
m1=h*j(t(i),s(i),c(i),r(i));
k2=h*f(t(i)+h/2,s(i)+k1/2,c(i)+l1/2,r(i)+m1/2);
l2=h*g(t(i)+h/2,s(i)+k1/2,c(i)+l1/2,r(i)+m1/2);
m2=h*j(t(i)+h/2,s(i)+k1/2,c(i)+l1/2,r(i)+m1/2);
s(i+1)=s(i)+k2;
c(i+1)=c(i)+l2;
r(i+1)=r(i)+m2;
%s(i+1)=s(i)+(k1+2*k2+2*k3+k4)/6;
%c(i+1)=c(i)+(l1+2*l2+2*l3+l4)/6;
%r(i+1)=r(i)+(m1+2*m2+2*m3+m4)/6;
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Runge Kutta Methods 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!