syms f(x)
Df = diff(f,x);
D2f = diff(f,x,2);
D3f = diff(f,x,3);
D4f = diff(f,x,4);
ode =3*D4f+(2*x^2+6)*D3f+5*D2f-Df-2*f == - 4*x^6+ 2*x^5 -55*x^4 - 24*x^3 - 22*x^2 - x*32;
cond1 = f(0)==0;
cond2 = Df(0)==1;
cond3 = D2f(0) == -8;
cond4 = D3f(0) == 6;
conds = [cond1 cond2 cond3 cond4];
fSol(x) = dsolve(ode,conds);
figure
ezplot(fSol(x),[0 1])
The error is :
Warning: Unable to find explicit solution.
> In dsolve (line 201)
(line 13)
Error using inlineeval (line 14)
Error in inline expression ==> matrix([])
Undefined function 'matrix' for input arguments of type 'double'.
Error in inline/feval (line 33)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in ezplotfeval (line 51)
z = feval(f,x(1));
Error in ezplot>ezplot1 (line 486)
[y, f, loopflag] = ezplotfeval(f, x);
Error in ezplot (line 158)
[hp, cax] = ezplot1(cax, f{1}, vars, labels, args{:});
Error in sym/ezplot (line 78)
h = ezplot(fhandle(f),varargin{:});%#ok<EZPLT>
Error in try2 (line 15)
ezplot(fSol(x),[0 1])

 Réponse acceptée

Star Strider
Star Strider le 31 Déc 2019
If an analytical solution is not an option, and a plot of the solution is the objectrive:
syms f(x) X Y
Df = diff(f,x);
D2f = diff(f,x,2);
D3f = diff(f,x,3);
D4f = diff(f,x,4);
ode =3*D4f+(2*x^2+6)*D3f+5*D2f-Df-2*f == - 4*x^6+ 2*x^5 -55*x^4 - 24*x^3 - 22*x^2 - x*32;
% cond1 = f(0)==0;
% cond2 = Df(0)==1;
% cond3 = D2f(0) == -8;
% cond4 = D3f(0) == 6;
[VF,Sbs] = odeToVectorField(ode);
odefcn = matlabFunction(VF, 'Vars',{x,Y});
[X,Y] = ode45(odefcn, [0 1], [0 1 -8 6]);
figure
plot(X, Y)
grid
legend(string(Sbs))
producing:
1fourth order differential equation - 2019 12 31.png

15 commentaires

Star Strider
Star Strider le 31 Déc 2019
kingcruises‘s ‘Answer’ —
I want to plot the solution coming out from this fourth order differential equation
Star Strider
Star Strider le 31 Déc 2019
@kingcruises —
What part of my Answer does not do what you want?
It plots the function and all the derivatives!
kingcruises
kingcruises le 31 Déc 2019
The code didn’t work still
Star Strider
Star Strider le 31 Déc 2019
What does ‘didn’t work’ mean, exactly?
The code I posted ran for me without error and produced the plot I posted. It is simply an extension of your code, using odeToVectorField to produce a column vector of first-order differential equations, and matlabFunction to create an anonymous function that ode45 then integrates to create the plot.
kingcruises
kingcruises le 31 Déc 2019
please can u provide me with matlab file '.m'
kingcruises
kingcruises le 31 Déc 2019
Error using symfun/subsref
Too many output arguments.
Error in (line 13)
[X,Y] = ode45(odefcn, [0 1], [0 1 -8 6]);
it gave this error
I do not understand the problems you are having with my code.
If you run my code exactly as I wrote it, you should have no problems with it. Note that ‘odefcn’ is the result of odeToVectorField first, followed by matlabFunction. You cannot use a symbolic function with ode45 or any of the other numeric solvers!
The anonymous function ‘odefcn’ is:
odefcn = @(x,Y) [Y(2); Y(3); Y(4); x.*(-3.2e+1./3.0)-((x.^2.*2.0+6.0).*Y(4))./3.0+Y(1).*(2.0./3.0)+Y(2)./3.0-Y(3).*(5.0./3.0)-x.^2.*(2.2e+1./3.0)-x.^3.*8.0-x.^4.*(5.5e+1./3.0)+x.^5.*(2.0./3.0)-x.^6.*(4.0./3.0)];
Just copy that from this Comment and paste it to your script.
Then run it as:
[X,Y] = ode45(odefcn, [0 1], [0 1 -8 6]);
figure
plot(X, Y)
grid
Sbs = {'f' 'Df' 'D2f' 'D3f'};
legend(string(Sbs))
That should work without problems.
kingcruises
kingcruises le 2 Jan 2020
can we get a code with dsolve as my initial code because I have to compare with the MOM "method of moments" solution. As the below figure I have to have the same figure but using dsolve
mom4th.PNG
Star Strider
Star Strider le 2 Jan 2020
Apparently, dsolve cannot integrate your differential equation. (I doubt that an analytic solution exists for it.) That is the reason we went with the numeric integration. The simplify function cannot simplify it to the extent that dsolve can solve it.
kingcruises
kingcruises le 2 Jan 2020
the code works well.
i want dsolve result same as mom solution. I can solve for second order but nor for fourth order
momvssdolve.PNG
Star Strider
Star Strider le 2 Jan 2020
I do not believe the fourth-order symbolic solution is possible, at least with the functions available in the Symbolic Math Toolbox.
kingcruises
kingcruises le 2 Jan 2020
what do you recommend to use that can solve the fourth order differential equation.
kingcruises
kingcruises le 2 Jan 2020
can method of moemnst give same graph as dsolve and how ?
kingcruises
kingcruises le 2 Jan 2020
hey i must plot ode as a total not f, D2f, Df, D3f each one alone.
I want to plot ode as one graph
The ‘method of moments’ was not part of my undergraduate or graduate education. (I had to look it up.) I will leave that part to you.
Plotting the total of the derivatives is straightforward. Only one change to my posted code is needed and that to sum across the columns in the plot call:
[X,Y] = ode45(odefcn, [0 1], [0 1 -8 6]);
figure
plot(X, sum(Y,2))
grid
That should do what you want.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by