Matlab - Escribir sistema ecuaciones en F.m

 
Vista:
sin imagen de perfil

Escribir sistema ecuaciones en F.m

Publicado por Juan José (60 intervenciones) el 11/05/2015 09:04:31
Buenas,

Dentro del marco métodos numéricos, y concretamente, runge-kutta 4 para sistemas de ecuaciones, me encuentro con que tengo un programa para éste, y no encuentro la forma de escribir la F.m para arrancar el programa.

Os adjunto el programa rks4.m.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function [T,Z]=rks4(F,a,b,Za, M)
 
%Input    - F is the system input as a string 'F'
%         - a and b are the endpoints of the interval
%         - Za=[x(a) y(a)] the initial conditions
%         - M is the number of steps
%Output  - T is the vector of steps
%        - Z=[x1(t) . . . xn(t)]where xk(t) is the approximation to the
%          kth dependent variable
 
% NUMERICAL METHODS: MATLAB Programs
%(c) 1999 by John H. Mathews and Kurtis D. Fink
%To accompany the textbook:
%NUMERICAL METHODS Using MATLAB,
%by John H. Mathews and Kurtis D. Fink
%ISBN 0-13-270042-5, (c) 1999
%PRENTICE HALL, INC.
%Upper Saddle River, NJ 07458
 
h=(b-a)/M;
T=zeros(1,M+1);
Z=zeros(M+1,length(Za));
T=a:h:b;
Z(1,:)=Za;
 
for j=1:M
   k1=h*feval(F,T(j),Z(j,:));
   k2=h*feval(F,T(j)+h/2,Z(j,:)+k1/2);
   k3=h*feval(F,T(j)+h/2,Z(j,:)+k2/2);
   k4=h*feval(F,T(j)+h,Z(j,:)+k3);
   Z(j+1,:)=Z(j,:)+(k1+2*k2+2*k3+k4)/6;
end

He tratado de escribir el archivo F.m de la siguiente forma:

1
2
3
4
5
function S=F
F1=@(x,y) 2*x+3*y;
F2=@(x,y) 2*x+y;
S={F1 F2};
end

Cuando no se trata de un sistema, y se trata de sólo una ecuación, es necesario una funcion handle para realizar un feval con ciertos valores posteriormente. He tratado de hacer lo mismo, aunque el error en este caso me aparece con el feval, además de que estoy con un cell array en el que tengo dos funciones handle.

Si trato de no realizar funciones handle en F.m, el problema lo encuentro en que la variable x e y no está definida.

1
2
3
4
5
function S=F(V)
x=V(1);
y=V(2);
S=[ 2*x+3*y 2*x+y];
end

¿Alguien tiene alguna idea de cómo hacer correr el programa??

Os escribo un ejemplo por si quereis ponerlo en marcha:
1
2
3
4
5
6
x'=2x+3y
y'=2x+y;
x(0)=-2.7
y(0)=2.8
[0,10]
h=0.05


Muchas gracias y un saludo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder