There used to be a function RefineMesh in Matlab used in solving ODE's, but it is now used for PDE's and isn't the same function. Is there a new function for the old purpose?

2 views (last 30 days)
Taylor Kimball
Taylor Kimball on 18 Nov 2021
Answered: J. Alex Lee on 23 Nov 2021
I'm looking at a textbook on Matlab, at the author used RefineMesh to define a time variable when solving an ODE using ode45. When I tried to run the code on my matlab, it said it didn't recognize RefineMesh. Did the name of the function change? I included the example below.
f = @(t,x) t.ˆ2 + x - x.ˆ4/10;
sol = ode45( f, [0,5], 0 );
% Compute and plot the relative residual on a lot of points
t = RefineMesh( sol.x, 40 );
[ z, dotz ] = deval(sol,t);
deltat = dotz./f(t,z)-1;
figure(1),semilogy(t,abs(deltat),'k.'),set(gca,'fontsize',24) xlabel('t'),ylabel('relative residual'),axis([0,5,1.0e-8,1.0])

Answers (1)

J. Alex Lee
J. Alex Lee on 23 Nov 2021
The workflow above only re-evaluates the "static" solution of ode45 at more data points by an interpolation scheme consistent with the ode45 discretization...you can achieve similar result by just calling ode45 with ode options with the "Refine" option set to some large value that gives you a satisfactory spacing.
There is a difference from running ode45 with tighter tolerances, which, all else equal, would force it to take smaller steps and thus in consequence your mesh will be more refined - but the answer would be different (more accurate) than "refining" the x-resolution after-the-fact.
Finally, a simple way to accomplish what you want might be to just split the differences equally in your x-vector (find all mid-points of you rexisting grid and include them). You could do something like
x = [1 2 4 7 9 10 11]
x = 1×7
1 2 4 7 9 10 11
NOld = numel(x);
% node splitting
s = x(1:end-1) + diff(x)/2;
t = reshape([x;[s,NaN]],1,[]);
t = t(1:end-1)
t = 1×13
1.0000 1.5000 2.0000 3.0000 4.0000 5.5000 7.0000 8.0000 9.0000 9.5000 10.0000 10.5000 11.0000
Or if you don't care about having the original nodes and want to refine with arbitrarily many new nodes, something like
% refine with arbitrarily many nodes
z = linspace(0,1,NOld);
NNew = 2*NOld;
t = interp1(z,x,linspace(0,1,NNew));
These solutions may not be the best, but hope can get you started.

Tags

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by