Hi, Im trying to write a simple script to solve a system of 2 ode\'s with built
ID: 1817472 • Letter: H
Question
Hi, Im trying to write a simple script to solve a system of 2 ode's with built in function ode45.I have created a function file and a script file to call the function and define variables.
this is the function file
function dxdt=damping(t,x)
dxdt1=x(2);
dxdt2=(1/m)*(W-b.*x(2)-k*x(1));
dxdt=[dxdt1;dxdt2];
and this is the script file
clear all
close all
clc
m=2;
k=1000;
W=196;
%Initial conditions for position and velocity
xo=[0, 0.1];
%Setting time span
tspan=[0 0.5];
[t,x]=ode45('damping',tspan,xo);
x(2),x(1)
However, when i run the program in the command window I get this error message telling me that variable that i have defined in the script file is undefined:
??? Undefined function or variable 'm'.
I have tried defining it first in the command window and defining as global variable but still get the same message.
Also, if i plug in a value for the variable m, i get the same error message but for the next variable in the progression.
Any help would be appreciated
thanks
Explanation / Answer
You need to put the constants in the function file. Your function file should be: function dxdt = damping(t,x) m=2; k=1000; W=196; b = ; dxdt1=x(2); dxdt2=(1/m)*(W-b.*x(2)-k*x(1)); dxdt=[dxdt1;dxdt2]; You are also missing a b term in there. The script file only needs you to pass xo and tspan to ODE45: clear all close all clc %Initial conditions for position and velocity xo=[0, 0.1]; %Setting time span tspan=[0 0.5]; [t,x]=ode45('damping',tspan,xo); X2 = x(2) X1 = x(1) Hope this helps.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.