Path: news.mathworks.com!not-for-mail From: "Douglas Nicolin" <douglas.nicolin@gmail.com> Newsgroups: comp.soft-sys.matlab Subject: Re: Writing a system of differential equations correctly Date: Wed, 17 Apr 2013 11:53:07 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 50 Message-ID: <kkm2f3$2se$1@newscl01ah.mathworks.com> References: <kkheo6$5hh$1@newscl01ah.mathworks.com> <kkiqq9$89m$1@newscl01ah.mathworks.com> <kkjc73$nc9$1@newscl01ah.mathworks.com> <kkkcga$bj6$1@newscl01ah.mathworks.com> <kklgfe$g4l$1@newscl01ah.mathworks.com> Reply-To: "Douglas Nicolin" <douglas.nicolin@gmail.com> NNTP-Posting-Host: www-05-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1366199587 2958 172.30.248.37 (17 Apr 2013 11:53:07 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Wed, 17 Apr 2013 11:53:07 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 4181250 Xref: news.mathworks.com comp.soft-sys.matlab:793724 "Torsten" wrote in message <kklgfe$g4l$1@newscl01ah.mathworks.com>... > > > > I'm still dealing with errors when running the code I developed. Actually the error arise because of a mistake of writing in my opinion. But I'm able to see what's wrong until now. My function is shown below: > > > > function DXDt = testsol(t,X) > > > > global N > > > > N = 50; > > r = 1; > > dr = r/N; > > Xeq = 1.7409; > > rhoss = 1057; > > rhoagua = 1000; > > Do = 3e-11; > > k1 = 1e-2; > > > > F(1) = 3*Do*exp(k1*X(1))/(X(N)^2)*((2*X(2)-2*X(1))/(dr)^2); > > > > for i = 2:N-2 > > F(i) = Do*exp(k1*X(i))/(X(N)^2)*(((X(i+1)-X(i-1))/(2*dr))*... > > ((2/(i*dr))+k1*(X(i+1)-X(i-1))/(2*dr))+(X(i+1)-2*X(i)+X(i-1))/(dr)^2)+... > > (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)*((X(i+1)-X(i-1))/dr); > > end > > > > for i = N-1 > > F(i) = Do*exp(abs(k1*X(i))/(X(N)))*(((Xeq-X(i-1))/(2*dr))*... > > ((2/(i*dr))+k1*(Xeq-X(i-1))/(2*dr))+(Xeq-2*X(i)+X(i-1))/(dr)^2)+... > > (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)^2; > > end > > > > F(N) = ((rhoss/rhoagua)*Do*exp(k1*X(N-1))/(X(N)))*(Xeq-X(N-2))/dr; > > > > DXDt = F'; > > > > The main problem is that the last differential equation, defined in F(N) is not part of the system generated after using finite differences in the pde. This ode is the last one to be added that I mentioned before. However, the variable X(N) must represent the dependent variable of the last ode and it must appear in the system as well. When I run the whole code for the solution of my function I get this error message: > > "??? Attempted to access X(50); index out of bounds because numel(X)=1." > > which is natural because X(N) is not calculated before. My problem is that X(N) is a variable that must appear in the whole system, but is also a dependent variable to be calculated by integration in time. That's the biggest mistake I'm making: I'm not sure how to write this function properly. > > You must solve your problem in _one_ complete system of ODEs, not by calling the ODE integrator twice - once for X and once for R (that's what I think you are trying to do ?). > Thus if the number of grid points is N, the dimension of the vector X and the dimension of the vector F from above must be N+1. Here, X(1) to X(N) represent your original solution variable X in the N grid points and element X(N+1) represents your solution variable R ; elements F(1) to F(N) represent the temporal derivatives of your solution variable X in the N grid points and element F(N+1) represents dR/dt. > > Best wishes > Torsten. You are wright. But in my system, the variable X(N) represents what would be my R(1) let's say. Once in the position N, in my case, there is the time derivative dRdt, I considered that the variable X(N) is my "R" variable that appears all over the system and in the last equation. That's my real problem. If my X(N) were a variable solve as a part of the system formed by my pde, there would be no errors on my code. But it's not. Adapting the indexes for the whole system is the problem. In other problems, when the dependence of the system on the X(N) variable does not exist it is easy to write the correct function. I'm search for examples similar to may case day by day and until now, I got nothing. I found many articles showing similar problems but solved in a different way. Kind regards Douglas