2nd order nonhomogenus differential equation in matlab

In summary, the conversation is about a student trying to solve a forced undamped oscillator with MATLAB using the equation y'' + ω0y = 2Cos(ωt). The student is stuck and receives an error message when running the solver. They share their m-file and ask for help. Another person points out a subtle error in line 7 and offers hints to help the student find the mistake. They also mention a possible logic error in line 7.
  • #1
Powertravel
9
0

Homework Statement



I am studying a forced undamped oscillator with MATLAB governed by the equation:

y'' + [itex]\omega[/itex]oy = 2Cos([itex]\omega[/itex]t)

First I have to write a function that can be solved by the solver ode45.

Here is where I am stuck. Matlab just spits error messages at me when I try to run the solver.
It would be awesome if someone could check my mfile and see where I am going wrong.

Homework Equations


y'' + [itex]\omega[/itex]oy = 2Cos([itex]\omega[/itex]t)
y(0)=0
y'(0)=0
[itex]\omega[/itex]o = 2
[itex]\omega[/itex] = 1.95+[itex]\epsilon[/itex]
e = a small number

The Attempt at a Solution



This is my m-file, descriptions in green
-----------------------------------------------
function [ydot] = occi( t,y ) % The name of the function is occi
Wo = 2; % The value of [itex]\omega[/itex]o
e = 9*8*10^-3; % Value for [itex]\epsilon[/itex]
w = 1.95+e; % The value for [itex]\omega[/itex]
ydot = zeros(2,1); %Creates a 2rows 1column matrix to contain the system of Ode
ydot(1) = y(1); %sets y' = y1
ydot(2) = 2*cos(w*t)-(Wo^2)*y; %sets Y'' = 2Cos([itex]\omega[/itex]t) - [itex]\omega[/itex]oy
end
------------------------------------------
Then I run this script:

>>[tout, yout] = ode45(@occi, [0 2*pi], [0; 0]);

and MATLAB slaps me in the face with:

? In an assignment A(I) = B, the number of elements in B and
I must be the same.

Error in ==> occi at 7
ydot(2) = 2*cos(w*t)-(Wo^2)*y;

Error in ==> odearguments at 98
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ==> ode45 at 172
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...



So, I don't get what's wrong. Thanks for help. (is it presumptuous to write that?)
 
Physics news on Phys.org
  • #2
As far as I can tell, you have only a single, fairly subtle error. Without meaning to frustrate you, I think it'd be better if you can find it for yourself. I'll give you a couple of hints:

1. The error is in line 7 (where you assign your function value to ydot(2) )
2. You could easily have made the exact same error in line 6, but didn't.

If you still struggle by tomorrow, I'll put you out of your misery.

Given the relevant equations you gave, it looks like you may also have a logic error in line 7. I don't think ω0 should be squared?
 

Related to 2nd order nonhomogenus differential equation in matlab

1. What is a 2nd order nonhomogeneous differential equation?

A 2nd order nonhomogeneous differential equation is a mathematical equation that involves a second derivative of a function, as well as non-zero terms that depend on the independent variable.

2. How does MATLAB handle 2nd order nonhomogeneous differential equations?

MATLAB has built-in functions and tools for solving 2nd order nonhomogeneous differential equations. These include the ode45 and ode23 functions, as well as the dsolve function for symbolic solutions.

3. Can you provide an example of solving a 2nd order nonhomogeneous differential equation in MATLAB?

Sure, here is an example of solving the 2nd order nonhomogeneous differential equation y'' + 2y' + y = 5 with the initial conditions y(0) = 1 and y'(0) = 0:

tspan = [0 10]; % set time intervaly0 = [1; 0]; % set initial conditions[t,y] = ode45(@(t,y) [y(2); 5-2*y(2)-y(1)], tspan, y0); % solve the differential equationplot(t,y(:,1)) % plot the solution

4. Can MATLAB also plot the solution of a 2nd order nonhomogeneous differential equation?

Yes, as shown in the previous example, MATLAB has functions for plotting solutions of differential equations. The ode45 function returns the solution as a vector, which can then be plotted using the plot function.

5. Are there any limitations to solving 2nd order nonhomogeneous differential equations in MATLAB?

While MATLAB is a powerful tool for solving differential equations, it may not always be able to find an analytical solution to a 2nd order nonhomogeneous differential equation. In these cases, numerical methods such as ode45 can still provide an approximate solution.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
872
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
331
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Calculus and Beyond Homework Help
Replies
21
Views
909
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
33
Views
3K
Replies
61
Views
1K
Back
Top