why do I get "Undefined function or variable 'tspan'."

I'm trying to solve for pendulum and everytime I try to run the file I get
Undefined function or variable 'tspan'.
function [z_ydot] = pendulum(t,z_y,k,d,m,L,g)
z_ydot = zeros(4,1);
z_ydot(1) = z_y(2);
z_ydot(2) = (k.*d^2.*(z_y(3)-z_y(1))./(m.*L^2) - ((g.*z_y(1))./L);
z_ydot(3) = z_y(4);
z_ydot(4) = (k.*d^2.*(z_y(1)-z_y(3))./(m.*L^2) - ((g.*z_y(3))./L);
clear; clc; close all
g=9.81; % acceleration due to gravity
m=2; % mass in kilograms
L=0.75; % length in metres
d=0.5; % distance to the spring in metres
k=130; % spring stiffness in N/m
timespan = [0 20]; % timespan in seconds
% z1=theta; z2=thetadot
y_z0 = [0 0 13 0];
[t,z_y] = ode45(@(t,z_y) pendulum(t,z_y,k,d,m,L,g), tspan, y_z0);
% time history of theta1
figure
plot(t,z_y(:,1))
title('Theta 1');
xlabel('time')
ylabel('theta')
grind on
% time history of theta2
figure
plot(t,z_y(:,3))
title('Theta 2');
xlabel('time')
ylabel('thetadot')
grind on

 Respuesta aceptada

Star Strider
Star Strider el 28 de Mzo. de 2020
The code defines that variable as ‘timespan’. The argument names and the varialble names must always match.
Try this:
[t,z_y] = ode45(@(t,z_y) pendulum(t,z_y,k,d,m,L,g), timespan, y_z0);

5 comentarios

I get another error
" Error in Untitled3>@(t,z)pendulum(t,z,k,d,m,L,g) "
I dont know whats wrong is it from the matlab it self or not
z_ydot(2) = (k.*d^2.*(z_y(3)-z_y(1))./(m.*L^2) - ((g.*z_y(1))./L);
1 0 1 2 3 2 3 21 2 1 23 4 32 1
The numbers indicate the number of active bracket nestings after the character they are below. You can see that at the end of the line, there is one active bracket nesting -- so you are missing one )
Star Strider
Star Strider el 28 de Mzo. de 2020
@Walter — Thank you!
finally it worked, thank you guys
Star Strider
Star Strider el 29 de Mzo. de 2020
Our pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Versión

R2015b

Etiquetas

Preguntada:

el 28 de Mzo. de 2020

Comentada:

el 29 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by