# How can i define algebraic equation in differential function in MATLAB?

I want to solve 7 pde's that are functions of time, radius(j) and length(i). I used the method of lines and converted them to a system of odes in time and it becomes something like this: $$dy/dt=((y(i,j)-y(i,j-1))\Delta{r} )+(y(i,j)-y(i-1,j))\Delta{x}$$ I define all of the equations in one function in MATLAB. This is my function: $$dy/dt=f(t,y)$$ and then I solve those ode's by using ode15s solver. Now one of the boundary conditions is an algebraic equation that should be in this function too. It is an algebraic equation of the form: $$y=f(r,x)$$ so I think if equations have the same appearance, I can write all of them in one function. So I write it in this way: $$(10^{-10})\times dy=y-f(r,x)$$ I put this small coefficient to eliminate $dy$ effect and solve algebraic equation, but MATLAB cant solve it. How can I define this function?

• I don't understand your notation. Are the functions $f(t,y)$ and $f(x,y)$ supposed to be the same? And how does the third equation follow from the first two? Finally, is $dy$ supposed to be some kind of time derivative? Please clarify. – Wolfgang Bangerth Oct 8 '15 at 12:05
• sorry. ok i explain it now. first i have 7 equations that are function of time and radius and lenght. i used method of line and covert PDEs equations to a system odes(dy/dt). this is one of the differential equations: dy(i,j)=(Aoepsilonf.*Ucpy(i,j))-(kgAo.*(y(1,1)-y(2,1))) and my algebraic equation is this: y(i,10)=((Deff/deltar)*(y(i,10)-y(i-1,10)))+kgAo*(y(2,10)-y(3,10)) so my odes are time derivatives.and (i,j)are notations for r and x. – fatemeh Oct 8 '15 at 13:38
• Hi Fatemeh. As Wolfgang says your notation and question in general is very confusing. If $dy=dy/dt$ then you should just write dy/dt. Also since the 7 differential equations are easy to write out (given you wrote one in your comment) I think you should just include them. You also multiply by 10^-10 to eliminate dy. How is this justified? In order for someone to answer this question I am afraid it will likely need to be significantly re-worked so that it is comprehensible. As it stands I have no idea what you are trying to do. – James Oct 8 '15 at 17:44
• thanks James.7 differential equations are the main equations.but algabreic equation is boundry condition.i dont know how use this algebraic equation with those diffrential equations? it was my idea that if the Appearance of equations are same i can write it in one function.because a algebraic equation dont have dy/dt term i think its better to use a small coefficient to eliminate its effect. – fatemeh Oct 8 '15 at 18:39
• @fatemeh, please edit the original question to make your intent clear. – Wolfgang Bangerth Oct 8 '15 at 20:06

Your question is not quite clear, but if your problem is roughly analogous to

$$\frac{d\mathbf{x}}{dt}=\mathbf{f}[\mathbf{x},y]$$ , $$g[\mathbf{x},y]=0$$

then you just need to use a mass matrix in your call to ode15s. This is how you "tell" Matlab that you have an algebraic equation.

That is, in Matlab pseudo-code you can express the above as

eye(n)*dxdt = f(x,y) % n=length(x)
0   *dydt = g(x,y) % y is a scalar


which you would pass to ode15s as a single system

M*dzdt = h(z) % here z=[x;y] , h=[f;g], and M = blkdiag(eye(n),0)


The particular syntax will depend on the specific system. The relevant part of the help in the above link begins where it says: "If the mass matrix $$M$$ is singular, then $$M(t,y)y′ = f(t,y)$$ is a system of differential algebraic equations."

(Edit: The explanation here may be more clear.)

• Thanks a lot. Yes this is my problem. I read about mas matrix. But its not quite clear. Can you explain how can i define this matrix in MATLAB? – fatemeh Oct 11 '15 at 17:54
• fatemeh - I gave a simple example of a mass matrix in my answer, and linked to the Matlab help page describing the syntax and options to use in calling ode15s. To be more specific, you would need to define your particular system in the question. That said, perhaps you should post this over at mathworks.com/matlabcentral/newsreader – GeoMatt22 Oct 11 '15 at 21:09