2nd order initial value problem in matlab

In summary, the function should take in two inputs (time,[initial values]) and it should output a vector of the time derivatives. The script that uses the function should plot the distance with respect to time using euler's method.
  • #1
epic325
2
0

Homework Statement



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

20*d[itex]^{2}[/itex]x;(dt[itex]^{2}[/itex])+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
  • #2
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: 573
  • #3
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

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




As a scientist, my first suggestion would be to double check your code and ensure that all the syntax and mathematical equations are correct. It's also important to make sure that the function odeFunc is being called correctly within the for loop. Additionally, you may want to try using a smaller time step (h) to see if that improves the accuracy of your plot. Another helpful tip would be to plot the derivative values (wVec(:,2)) to see if they are changing as expected. This can help identify any errors in the calculation of the derivatives. Finally, it may be helpful to consult with a colleague or seek assistance from a tutor or online forum for further troubleshooting. Good luck with your assignment!
 

1. What is a 2nd order initial value problem in Matlab?

A 2nd order initial value problem in Matlab is a mathematical equation that involves a second-order derivative and requires two initial conditions to be solved. It is represented as a function with the independent variable, the dependent variable, and the two initial conditions.

2. How do I solve a 2nd order initial value problem in Matlab?

To solve a 2nd order initial value problem in Matlab, you can use the built-in function "ode45" which is specifically designed for solving ordinary differential equations. You will need to define the function, initial conditions, and time span as inputs to the function.

3. What is the difference between a 1st and 2nd order initial value problem?

The main difference between a 1st and 2nd order initial value problem is the number of initial conditions required. A 1st order problem only requires one initial condition, while a 2nd order problem requires two initial conditions. Additionally, a 2nd order problem involves a second-order derivative, making it more complex.

4. Can I plot the solution to a 2nd order initial value problem in Matlab?

Yes, you can plot the solution to a 2nd order initial value problem in Matlab using the "plot" function. You will need to define the time span and the solution vector as inputs to the function. This will give you a visual representation of the solution over time.

5. Are there any tips for solving a 2nd order initial value problem in Matlab?

Some tips for solving a 2nd order initial value problem in Matlab include properly defining the function and initial conditions, choosing an appropriate time span, and checking for errors in the code. It is also helpful to break down the problem into smaller steps and solve them one at a time.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
812
  • Engineering and Comp Sci Homework Help
Replies
1
Views
949
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
801
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
866
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top