Cookbook: Difference between revisions

Jump to navigation Jump to search
177 bytes added ,  22 August 2012
Line 80: Line 80:
==== Solution ====
==== Solution ====


<code>
We could solve the problem with the following code
 
t = linspace (0, 10, 100);
t = linspace (0, 10, 100);
xprime = @(x, t, k) - k * x;
function sprime = spring (s, t, k)
  x = s(1);
  v = s(2);
  sprime(1) = v;
  sprime(2) = -k * x;
endfunction
k = 1;
k = 1;
x1 = lsode (@(x, t) xprime (x, t, k), 1, t);
x1 = lsode (@(x, t) spring (x, t, k), [1;0], t)(:, 1);
k = 2;
k = 2;
x2 = lsode (@(x, t) xprime (x, t, k), 1, linspace (0, 10, 100));
x2 = lsode (@(x, t) spring (x, t, k), [1;0], t)(:, 2);
plot
plot (t, x1, t, x2)
</code>
legend ('x1', 'x2')
 
 
[[File:solparfun.png]]


==== Discussion ====
==== Discussion ====
349

edits

Navigation menu