Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this problem that i would like to solve preferably in MATLAB


$$\frac{d^2y}{dx^2} + \sin(y) = 0$$

with endpoint conditions $y(0) = 0$ and $y(0.5) = 0.2618$.

I think i have to plot the solution. I would appreciate any kind of help.

My incomplete solutions until now is:

a=0;b=10; %limits
h=0.00001; %step

N=round((b-a)/h)+1; %nodes

x=zeros(N,1);% init solution vectro

x(1)=0; %initial condition

% y(1) = 0.2618; - i still need the second condition

for i=2:N
    y(i)=y(i-1)- h * sin(x(i-1));

plot(a:h:b, x);
hold on;
share|cite|improve this question

If you want a numerical solution, you can use a shooting method. It works like this. Choose $\alpha\in\mathbb{R}$ and solve by your favourite numerical method the problem $$ y''+\sin(y)=0,\quad y(0)=0,\quad y'(0)=\alpha $$ on the interval $[0,1/2]$. The value found at $x=1/2$ is a function of $\alpha$ that I will denote by $s(\alpha)$. To solve the problem, you must find $\alpha$ such that $s(\alpha)=0.2618$. It is clear that $s(0)=0$. Find a value of $\alpha$ such that $s(\alpha)>0.2618$ and apply for instance the bisection method to find an approximate solution of $s(\alpha)=0.2618$.

In Mathematica you can get the solution as


The command Plot[u[x],{x,0,.5},AspectRatio->Automatic] gives the plot enter image description here

Finally, multiplying the equation by $y'$ and integrating once we get $$ \frac12\,(y')^2+(1-\cos(y))=C,\quad C\ge0, $$ from where $$ \frac{dy}{\sqrt{2\,C-(1-\cos(y))}}=\pm\,dx, $$ which can be integrated in terms of elliptic functions.

share|cite|improve this answer
up vote 0 down vote accepted

So i search a bit more on the internet for a matlab solution and i found this great video

This really explains on the same kind of problem that i have. Following his examples I tried to adjust his code to my problem and i got this (im not sure 100% is corect - but at least it plots the same plot as Julian's)

xlow = 0; xhigh = 0.5;
solinit = bvpinit(linspace(xlow,xhigh,100), [1 -1]);
sol = bvp4c(@exam1ode, @exam1bc, solinit);
xint = linspace(xlow, xhigh);
Sxint = deval(sol,xint);
plot(xint, Sxint(1,:));

exam1ode defines my ecuation

function f1 = exam1ode(x, y)
f1 = [y(2) -sin(y(1))];

exam1bc defines the boundary conditions

function f2 = exam1bc(ya, yb)
f2 = [ya(1) yb(1)-0.2618];
share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.