Solving An IVP on Matlab with ODE 45 with different tolerances

In summary, the conversation is about a code that is giving an error on line 19 saying "unrecognized ivpfun." The person is looking for help in fixing the error as they are unsure of how to proceed. They are also discussing defining a function called "ivpfun" and are using their professor's guide as a reference.
  • #1
ver_mathstats
260
21
Homework Statement
Integration of a problem using Matlab ode45 and matlab ode23, we need to see it for a variety of different error tolerances. The numbers are below, I've included a picture to keep it neater.
Relevant Equations
Matlab ODE45
Screen Shot 2022-12-05 at 1.18.19 PM.png


My code is as follows: but when I use the function in my command window exactsol(t) and input a tolerance but there is an error in LINE 19 saying unrecognized ivpfun, could someone help me fix it as I am unsure of how to proceed from here.

Matlab:
function y = exactsol(t)
y = zeros (2,1);
y(1) = exp(-0.1*t) + exp(-200*t);
y(2) = exp(-200*t);
clear;
clf
global no_fcn;
T0 = [0 100];
y0 = [2, 1];
tol = 1;
fprintf ('*** \t ode45 \t ***\n');
fprintf ('tol\t error\t steps\t ave step \t fcn eval\t time \n');
for i=1:12
    no_fcn = 0;
    tol = tol/10;
    tolerance(i) = tol;
    options=odeset('AbsTol', tol);
    ts = cputime;
    [t,y]=ode45(@ivpfun, T0, y0, options);
    time(i) = cputime - ts;
    fcn_eval(i) = no_fcn;
    error (i) = norm(y(end,:) - exactsol(t(end)));
    h = t(2:end) - t(1:end-1);
    if (i == 7)
        plot (t(1:end -1), h)
        ylabel ('stepsize');
        xlabel ('t');
        title ('ode45, tol = 1e-7');
        print ’ode45.ps’
    end
    steps(i) = length(h);
    ave_step(i) = sum(h)/steps(i);
    fprintf ('% .0e\t%.2e\t% 5d\t% .2e\t% 5d \t %.1e\n', tolerance(i), error(i), steps(i), ave_step(i), fcn_eval(i), time(i));
end
tol = 1;
fprintf ('*** \t ode23s \t ***\n');
fprintf ('tol\t error\t steps\t ave step \t fcn eval\t time \n');
for i=1:12
    no_fcn = 0;
    tol = tol/10;
    tolerance(i) = tol;
    options=odeset('AbsTol', tol);
    ts = cputime;
    [t,y]=ode23s(@ivpfun, T0, y0, options);
    time(i) = cputime - ts;
    fcn_eval(i) = no_fcn;
    error (i) = norm( y(end ,:)'- exactsol( t(end) ) );
    h = t(2:end) - t(1:end -1);
    if (i == 7)
        plot (t(1:end -1), h)
        ylabel ('stepsize');
        xlabel ('t');
        title ('ode23a, tol = 1e-7');
        print ’ode23.ps’
    end
    steps(i) = length(h);
    ave_step(i) = sum(h)/steps(i);
    fprintf ('% .0e\t%.2e\t% 5d\t% .2e\t% 5d \t %.1e\n', tolerance(i), error(i),steps(i), ave_step(i), fcn_eval(i), time(i));
end
 
Physics news on Phys.org
  • #2
You haven't defined ivpfun. Read the documentation for examples of how to define a function.
 
  • #3
pasmith said:
You haven't defined ivpfun. Read the documentation for examples of how to define a function.
Yes sorry I was looking at that afterwards, I think I'm just struggling with now where to define it. function dydt = ivpfun(t,y) would be my function right? Sorry I am using my professors guide as an example so that's why I am a little confused.
 

FAQ: Solving An IVP on Matlab with ODE 45 with different tolerances

What is an IVP and how does it relate to solving equations on Matlab?

An IVP (initial value problem) is a type of mathematical problem that involves finding a function that satisfies a given equation, along with a set of initial conditions. In Matlab, solving an IVP involves using the ODE45 function, which is a built-in solver for ordinary differential equations (ODEs).

What is ODE45 and how does it work?

ODE45 is a function in Matlab that uses a Runge-Kutta method to solve ODEs numerically. It works by breaking down the problem into smaller time steps and using a combination of previous and current values to approximate the solution at each step. This allows for more accurate results compared to other methods.

How do different tolerances affect the solution when using ODE45?

The tolerance in ODE45 refers to the maximum error allowed in the solution. A lower tolerance means that the solver will take smaller time steps and produce a more accurate solution, but it may also take longer to compute. On the other hand, a higher tolerance may result in a faster computation time, but the solution may be less accurate.

Can I change the tolerance in ODE45 while the solver is running?

Yes, it is possible to change the tolerance while the solver is running. This can be done by using the "odeset" function to specify the desired tolerance before calling ODE45. It is important to note that changing the tolerance may affect the accuracy and speed of the solution.

Are there any other options for solving an IVP on Matlab besides ODE45?

Yes, there are other built-in solvers in Matlab for solving ODEs, such as ODE23, ODE113, and ODE15s. These solvers use different algorithms and have different capabilities, so it is important to choose the appropriate solver based on the specific problem at hand. Additionally, there are also external solvers available for download from the Matlab File Exchange.

Similar threads

Back
Top