Solving ODEs in Matlab without ODE45 or Built-In Functions

  • MATLAB
  • Thread starter arizonian
  • Start date
  • Tags
    Matlab
In summary, I am having trouble with some Matlab code that I am writing. I am attempting to solve ODE writing my own code, per the Professor. That means I cannot use ODE45 or other built-in functions. I am looking for help with solving the equations.
  • #1
arizonian
18
2
I am having trouble with some Matlab code that I am writing. I am attempting to solve ODE writing my own code, per the Professor. That means I cannot use ODE45 or other built-in functions.

My problem: Oiler, a play on Euler, calls another function, FX, that contains the two equations that I am attempting to solve. Oiler is called from the command line by the operator (me).

Command line:

[X,Y] = oiler('bloodtest',2,3,3,4)

Function # 1:

function [rhs] = oiler(FX,a,b,n,h)
r=[a;b]
s=ones(1,n)
t=ones(1,n)
rhs=[s;t]
rhs(:,1)=feval(FX,r);
for i=2:n;
rhs(:,i)=rhs(:,i-1)+feval(FX,rhs(:,i-1))*h
i=i+1;
end

Function # 2:

function [dx,dy] = bloodtest(x,y)
[dx,dy]=[x.^2;2.*y];


I get error flags saying 'too many outputs' for function #2, but I have not yet found a way to call the second function using a vector as the argument.

Any help will be appreciated

Bill
 
Last edited:
Physics news on Phys.org
  • #2
arizonian said:
Function # 2:

function [dx,dy] = bloodtest(x,y)
[dx,dy]=[x.^2;2.*y];<----LOOK HERE


I get error flags saying 'too many outputs' for function #2, but I have not yet found a way to call the second function using a vector as the argument.

Hi Bill, I discovered what is reddened in the quoted text. Change the ";" in the second term by ",". Maybe that is giving you some trouble.
 
  • #3
I think the problem is here:

rhs=[s;t]
rhs(:,1)=feval(FX,r);

in the first line you make the variable rhs a matrix with two rows and 3 columns, namely:
[tex]\left(\begin{array}{ccc}
1&1&1\\
1&1&1\end{array}
\right)[/tex]
but in the next line you say: assign the result of feval(FX,r) to "all rows of the first column" of rhs. But that is a matrix with two rows and one column, while FX (bloodtest) will return a matrix with one row and two columns.

I think you should change FX (bloodtest) to:

Function # 2:

function [dx; dy] = bloodtest(x,y)
[dx; dy]=[x.^2;2.*y];
 
  • #4
Got past the first problem, but I am stuck on another problem. The indices don't seem to be working like I think they should.

The equations of interest from the homework assigned:
dy/dx = -2y+5*exp(-x) % (y1)
dz/dx = (-y*z^2)/2 % (y2)

I am incrementing by 0.2. They are coupled, so vector algebra comes into play. Now, on to the code that I am writing.

Command line:

EDU>> e=oiler('P25_11',0,2,4,5,.2)

Function # 1 %%%%%%%%%%%%%%%%%%%%%%%%%

function [rhs] = oiler(FX,xo,y1,y2,n,h)

rhs=ones(2,n);
rhs(:,1)=[y1;y2];
x=xo+h;
% rhs(:,1)=feval(FX,x,rhs(1,1),r(2,1));
% rhs(:,1)=feval(FX,xo,yo,zo);
for k=2:n;
rhs(:,k)=rhs(:,k-1)+(feval(FX,x,rhs(1,k-1),rhs(2,k-1)))*h;
x=x+h;
end

Function # 2 %%%%%%%%%%%%%%%%%%%%%%%

function[dy1y2]=P25_11(x,y1,y2)
dy1y2=[-2*y1+5*exp(-x);(-y1*y2^2)/2];


and the response is:

e =

2.0000 2.0187 1.8816 1.6777 1.4560
4.0000 0.8000 0.6708 0.5861 0.5285


Maybe the response is correct for what I am trying to do, but computing by hand, the first term on the bottom line should be -16. That means the entire bottom row is wrong.

Thanks again

Bill
 
  • #5
Maybe my understanding of differential equations with boundaries is suspect. The code does run, the incrementation is taking place like it should. I did compute the second term and it is correct. The third term is correct, and the fourth, and the fifth.

I have egg on my face.

Bill
 

FAQ: Solving ODEs in Matlab without ODE45 or Built-In Functions

How can I solve ODEs in Matlab without using ODE45 or other built-in functions?

There are several methods for solving ODEs in Matlab without using built-in functions. One approach is to convert the ODE into a system of first-order differential equations and use the Euler's method or Runge-Kutta method to numerically solve the system. Another option is to manually code the numerical algorithm for solving the ODE, such as the Adams-Bashforth or Adams-Moulton methods.

What are the advantages of solving ODEs without built-in functions?

Solving ODEs without built-in functions allows for more flexibility and customization in the numerical algorithm used. It also provides a better understanding of the underlying mathematical principles and allows for more control over the accuracy and stability of the solution.

Are there any limitations to solving ODEs without built-in functions?

Yes, solving ODEs without built-in functions can be more time-consuming and require a deeper understanding of numerical methods and programming. It may also be less efficient for solving complex ODEs with high precision requirements.

How do I choose the appropriate numerical method for solving ODEs without built-in functions?

The choice of numerical method depends on the specific ODE being solved and the desired level of accuracy. It is important to consider the stability and convergence properties of the method as well. Consulting with a numerical analysis textbook or seeking guidance from a mentor or professor can also be helpful in selecting an appropriate method.

Can I combine different numerical methods when solving ODEs without built-in functions?

Yes, it is possible to combine different numerical methods, such as using a higher-order method for the initial time steps and switching to a lower-order method for subsequent steps. This approach can provide better accuracy and stability for certain types of ODEs. However, it may also require more programming and computational effort.

Similar threads

Replies
10
Views
2K
Replies
5
Views
2K
Replies
9
Views
3K
Replies
8
Views
2K
Replies
1
Views
2K
Replies
2
Views
3K
Replies
2
Views
3K
Back
Top