MATLAB: Ode,dsolve,spline,ode interp, differantial

devaldifferantial equationsdsolveinterp1ode45spline

x=[0:0.5:3]
M=[0 1 4.5 12.75 25 41 62]
dQ/dx=M
How can i solve this diff equation. My code is below
x=[0:0.5:3]
M=[0 1 4.5 12.75 25.1 41 62]
k=spline(x,M)
[x,Q] = ode45(@(x,Q) k, [0 3], 0);
it does not work
I dont have function ı have data x and data M. I use these datas with spline.
is there any way to solve this question in ODE, dsolve, deval or anything else?

Best Answer

You are not asking to do anything more than to integrate a spline, and perhaps to plot the result as a function of x. (You don't really say what you wanted to do with it.)
If you have the curve fitting toolbox, then fnint will do the integral directly, with the result as another higher order spline. So
x = [0:0.5:3];
M = [0 1 4.5 12.75 25 41 62];
spl = spline(x,M);
splint = fnint(spl);
fnplt(splint)
So these lines are suffcient to do what you wanted. Note the use of semi-colons at the end of lines.
By the way, your code will improve when you begin to use variable names that have some meaning in context. So instead of naming a spline k, use a descriptive name. Then when you need to debug your code next month or next year, you will be able to read your own code more easily.
If you don't have the curve fitting toolbox, or are not allowed to use fnint and fnplt, or you really want to use ode45, that too is not difficult. You need to learn then to use functions properly, and how to pass in arguments to functions.
x = [0:0.5:3];
M = [0 1 4.5 12.75 25 41 62];
spl = spline(x,M);
odefun = @(t,y) ppval(spl,t);
y0 = 0;
xspan = linspace(x(1),x(end),100);
[xout,yout] = ode45(odefun,xspan,y0);
plot(xout,yout)
Related Question