write a differential equation in standard form in matlab

To express a differential equation in standard form in Matlab, we can use the function diff to take symbolic derivatives of the dependent variable(s) with respect to the independent variable.

For example, consider the differential equation:

y'' - 2y' + y = 0

To express this equation in standard form, we first define the dependent variable y and the independent variable x as symbolic variables:

main.m
syms y(x)
10 chars
2 lines

Then, we use diff to take the first and second derivatives of y with respect to x:

main.m
dydx = diff(y, x);
d2ydx2 = diff(y, x, 2);
43 chars
3 lines

Finally, we substitute these derivatives and the original function y into the differential equation, and simplify:

main.m
ode = d2ydx2 - 2*dydx + y == 0;
ode = simplify(ode)
52 chars
3 lines

This returns the differential equation in standard form:

y(x) - 2*diff(y(x), x) + diff(y(x), x, x) = 0

gistlibby LogSnag