# How to plot a derivative against variable?

10 visualizaciones (últimos 30 días)
River el 1 de Abr. de 2023
Respondida: Peter Perkins el 5 de Abr. de 2023
Use if statements to select an equation for x based on the inputed value for damping. Plot a graph of x (displacement) against time (this bit works). Then want to differentiate x for velocity, and differentiate velocity for acceleration and then plot all three. I used:
syms t
v = diff(x,1)
plot(t,v)
Get error "Data must be numeric, datetime, duration, categorical, or an array convertible to double."
x is a previously selected equation with t as a variable, also includes Z but this is asked for and inputted at the beginning.
##### 2 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
River el 1 de Abr. de 2023
Editada: Walter Roberson el 1 de Abr. de 2023
%% Program to simulate the system by giving graphs for displacement, velocity and acceleration with respect to time.
% Selects equation and calculates displacement based on the given damping
% ratio.
clear variables
% Ask what the damping ratio is
Z = input('What is the damping ratio Z?');
t = (0.0:0.001:5);
% Equation for undamped
if Z == 0
x = (0.01.*cos(595.6.*t)) + (0.1599.*cos((590.*t)-0.3886));
% Equation for underdamped
elseif (0 < Z) && (Z<1)
x =(exp(-595.60.*Z.*t).*((0.01.*cos(595.6.*t.*sqrt(1-(Z^2)))) + (((0.01.*Z)./(sqrt(1-(Z^2)))).*sin(595.6.*t.*sqrt(1-(Z^2)))))) + (0.1599.*cos((590.*t)-0.3886));
% Equation for critically damped
elseif Z == 1
x = ((0.01 + 5.966.*t).*(exp(-595.6.*t))) + (0.1599.*cos((590.*t)-0.3886));
% Equation for overdamped
elseif Z > 1
x = (((0.0017 + Z + sqrt(Z^2 - 1))./(200*sqrt(Z^2 - 1)))*exp((-595.6*Z + 595.6*sqrt(Z^2 - 1))*t)) + ((0.01 - ((0.0017 + Z + sqrt(Z^2 - 1))./(200*sqrt(Z^2 - 1))))*exp((-595.6*Z + 595.6*sqrt(Z^2 - 1))*t)) + (0.1599.*cos((590.*t)-0.3886));
% Ask for a positive number if a negative one is entered
else
disp('Please enter a positive value for Z.')
end
plot(t,x,'b')
grid on
grid minor
xlabel('Time (s)')
ylabel('Displacement (m)')
title('Displacement of mass in micro-energy harvester')
% Differentiate displacement for velocity and display a graph agaist time.
syms t
v = diff(x,1);
plot(t,v)
this is the complete code

Iniciar sesión para comentar.

Walter Roberson el 1 de Abr. de 2023
Change
t = (0.0:0.001:5);
to
syms t
Change
plot(t,x,'b')
to
fplot(x, [0 5])
change
plot(t, v)
to
fplot(v, [0 5])
##### 1 comentarioMostrar NingunoOcultar Ninguno
River el 1 de Abr. de 2023
thank you

Iniciar sesión para comentar.

### Más respuestas (1)

Peter Perkins el 5 de Abr. de 2023
For extra credit, create a timetable from displacement, add velocity and accel, then use stackedplot to plot all three together.
##### 0 comentariosMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Numeric Solvers en Help Center y File Exchange.

R2023a

### Community Treasure Hunt

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

Start Hunting!