i need solve m*d2x/dt2 + c*dx/dt + k*x= 0 with matlab and I have encountered an error Undefined function or variable 't'. please help me

6 次查看(过去 30 天)
function dx = kc(t,x)
global cm km
dx = [-cm*x(1) - km*x(2);x(1)];
tspan = [0 15];
ic = [0 1];
global cm km
m = 20; c(1)=5; c(2)= 40; c(3) = 200; k = 20;
km=k/m;
for n = 1:3
cm = c(n)/m;
[t,x] = ode45('kc',tspan,ic);
plot(t,x(:,2)); grid;
xlabel('time - sec.');
ylabel ('displacement-m');
title('m*(d2x/dt2)+c*(dx/dt)+k*x=0; m=10 kg, k=40 N/m')
hold on
end

回答(1 个)

James Tursa
James Tursa 2019-4-22
编辑:James Tursa 2019-4-22
Is this code all together in one file as written above? You need to have your driver code (the code that calls ode45) first, and your derivative function code last or in another file. The ode45 function call should not be inside your derivative function.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by