second order differential equation with variable coefficient

3 views (last 30 days)
Hi,
As wrote in a previous question I have to solve the equation: d^2x/dt^2= (q/m)(E/v^2)
I have wrote the following code:
q=1; m=1; v=1; E=1;
rhs=@(z,x)[x(2);(q/m)*(E/v^2)];
[za, xa] = ode45(rhs, [0 100], [0 0]);
and it works fine.
The problem is that E for me is a function of z that I can write as:
Ez=zeros(1,100);
for i=1:1:100
if (i<=10 || i>=50)
Ez(i)=0;
else
Ez(i)=1;
end
end
I have found an example here: http://www.mathworks.it/help/techdoc/ref/ode23.html where is shown how to solve ode with time-dependent terms, but is is about a first order problem and I'm not able to write something similar for my second order problem... could you help me?

Answers (0)

Community Treasure Hunt

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

Start Hunting!