MATLAB Answers

ode,dsolve,spline,ode interp, differantial

4 views (last 30 days)
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?

  0 Comments

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 4 Aug 2020
Edited: John D'Errico on 4 Aug 2020
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)

More Answers (1)

Alan Stevens
Alan Stevens on 4 Aug 2020
More like this perhaps:
xspan = [0 3];
Q0 = 0;
[x, Q] = ode45(@f, xspan, Q0);
plot(x,Q), grid
xlabel('x'), ylabel('Q')
function dQdx = f(x,~)
X=0:0.5:3;
M=[0 1 4.5 12.75 25 41 62];
dQdx = spline(X,M,x);
end

Community Treasure Hunt

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

Start Hunting!