2nd order initial value problem in matlab

Click For Summary

Discussion Overview

The discussion revolves around solving a second-order initial value problem related to a damped spring using MATLAB. Participants are focused on writing a function to compute time derivatives and a script to plot the results using Euler's method.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster (epic325) describes their attempt to implement a MATLAB function for a damped oscillator and expresses difficulty in plotting the expected results.
  • Some participants propose corrections to the code, suggesting that the function should be called with the correct initial values in the loop.
  • Another participant shares their successful implementation in Octave, providing an alternative function and plotting method.
  • There is a discussion about the correct way to update the state vector in the loop, with one participant questioning the use of 'ans' in the original code.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections in the original code, particularly regarding how the function is called and how the state vector is updated. However, there are no explicit agreements on the overall approach or final implementation details.

Contextual Notes

Limitations include potential misunderstandings of MATLAB syntax and the specific implementation of Euler's method. The discussion does not resolve whether the original approach or the alternative provided is superior.

Who May Find This Useful

Readers interested in numerical methods for solving differential equations, particularly in the context of physics and engineering applications, may find this discussion relevant.

epic325
Messages
2
Reaction score
0

Homework Statement



1.)I want to write a function in MATLAB that contains the 2nd order function:

20*d^{2}x;(dt^{2})+5*dx/dt + 20*x=0 (dampened spring)

-The function should have 2 inputs (time,[initial values]) initial values should be a vector of 2 values
-The function should output a vector of the time derivatives

2.)I want to write a script that that uses the function to plot the function using euler's method with a for loop.


Homework Equations


I solved for the time derivatives

[w(2) ; -.25w(2)-w(1)] where my initial values are [1; 0] x(0)=1 and dx/dt(0)=0



The Attempt at a Solution


I think I have figured the code out but it's not plotting the right thing. it should be a dampened oscillator, but its plotting a line. I just need some help with the debugging. I will paste the function code followed by the separate script:

%This is the function where the input will be t and wVec=[initial values]

function wDerVec = odeFunc(t,wVec)
wDerVec=[wVec(2) -.25*wVec(2)-wVec(1)];
end

clear
clc
h=0.1; %h is the time step (.1s)
t=0:h:15; %time interval (0 to 15 s)
wVec=[1 0]; %initial condition

for i=2:length(t),
odeFunc(t,wVec);
wVec(i,:)=wVec(i-1,:)+ans*h;
end

plot(t,wVec(:,1)); %plots the distance with respect to time
title('Euler Approximation, damped spring');
xlabel('Time');
ylabel('distance, x');


I'm not very good at MATLAB so any help would be greatly appreciated! Thanks!
 
Physics news on Phys.org
Good Afternoon epic325,

I tried your equation in Octave and had no problem. Here is the sequence of commands I used.

Code:
function Y=odeFCT(x,t)
Y=zeros(2,1);
Y(1)=x(2);
Y(2)=(-1/20)*(5*x(2)+20*x(1));
endfunction
t=linspace(0,50,1001);
y=lsode(@odeFCT,[1 0]',t);
plot(t,y(:,1))

Here is the graph obtained (plotted with gnuplot instead of Octave.)

Let me dive into your code.
 

Attachments

  • dampened-spring.png
    dampened-spring.png
    9.9 KB · Views: 662
Quick question -

In your code, you have

Code:
for i=2:length(t),
odeFunc(t,wVec);
wVec(i,:)=wVec(i-1,:)+ans*h;
end

Shouldn't it be

Code:
for i=2:length(t),
odeFunc(t,wVec(i-1,:));
wVec(i,:)=wVec(i-1,:)+ans*h;
end

?
 
Yes, jfgobin. That was the problem. Thank you very much for your help!
 

Similar threads

Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K