2nd order nonhomogenus differential equation in matlab

Click For Summary
SUMMARY

The forum discussion focuses on solving a second-order non-homogeneous differential equation for a forced undamped oscillator using MATLAB's ode45 solver. The user encountered an error related to matrix dimensions when attempting to assign values in their function file. The equation being solved is y'' + ω₀y = 2Cos(ωt), with initial conditions y(0)=0 and y'(0)=0. The user’s function, named 'occi', contains a logic error where ω₀ is incorrectly squared in the assignment for ydot(2).

PREREQUISITES
  • Understanding of second-order differential equations
  • Familiarity with MATLAB programming and function definitions
  • Knowledge of MATLAB's ode45 solver and its syntax
  • Basic concepts of forced oscillators in physics
NEXT STEPS
  • Review MATLAB function syntax and best practices for defining ODEs
  • Learn about error handling in MATLAB to debug function files
  • Study the mathematical principles behind forced undamped oscillators
  • Explore MATLAB's documentation on ode45 and its parameters
USEFUL FOR

This discussion is beneficial for students and researchers in physics and engineering, particularly those working with differential equations and numerical methods in MATLAB.

Powertravel
Messages
9
Reaction score
0

Homework Statement



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

y'' + \omegaoy = 2Cos(\omegat)

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'' + \omegaoy = 2Cos(\omegat)
y(0)=0
y'(0)=0
\omegao = 2
\omega = 1.95+\epsilon
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 \omegao
e = 9*8*10^-3; % Value for \epsilon
w = 1.95+e; % The value for \omega
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(\omegat) - \omegaoy
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
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?
 

Similar threads

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