Plot x(t) by calling a function
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Fatemeh Salar
el 10 de Jul. de 2022
Respondida: Fatemeh Salar
el 10 de Jul. de 2022
Hi.
I tried to write a function and then call it to plot me y , it didn;t work out . could you please tell me what is wrong with it ?
Here is the script when I tried to call it and then plot it :
clc
clear
close all
m=input('Please enter mass : ')
c=input('Please enter damping : ')
k=input('Please enter stifness : ')
x0=input('Please enter x at t=0 : ')
v0=input('Please enter v at t=0 : ')
t=input('Please enter time : ')
[y]=myfunn(m,c,k,x0,v0,t);
t=linspace(0,t,100);
plot(t,y)
xlabel('time')
ylabel('x(t)')
grid on
hold on
Here is the function :
function [y]=myfunn(m,c,k,x0,v0,t)
w0=sqrt(k/m);
zeta=c/(2*sqrt(k*m));
if(zeta>0 && zeta<1)
y=(exp(zeta.*(-1).*t)).*((x0.*cos(w0.*sqrt(1-zeta.^2).*t))+(v0+zeta.*w0.*x0).*sin(sqrt(1-zeta.^2).*t)./(w0.*sqrt(1-zeta.^2)));
elseif(zeta==1)
y=(exp(zeta.*(-1).*t)).*(x0+t.*(zeta.*w0.*x0));
elseif(zeta>1)
y=(exp(zeta.*(-1).*t)).* ...
(((v0+(zeta+sqrt(zeta.^2-1)))).*exp(w0.*sqrt(zeta.^2-1).*t)./2.*w0.*sqrt(zeta.^2-1) + ...
((-v0+(-zeta+sqrt(zeta^2-1))))*exp(-1*w0.*sqrt(zeta.^2-1).*t)./2.*w0.*sqrt(zeta.^2-1));
end
end
Maybe I am wrong in calling a function.... Idk .... because the result will be like this (and ain't plot anything)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1060360/image.png)
Respuesta aceptada
KSSV
el 10 de Jul. de 2022
You are entering t as single number into the function. Try this:
clc
clear
close all
m=input('Please enter mass : ') ;
c=input('Please enter damping : ') ;
k=input('Please enter stifness : ') ;
x0=input('Please enter x at t=0 : ') ;
v0=input('Please enter v at t=0 : ') ;
t=input('Please enter time : ') ;
t=linspace(0,t,100);
y=myfunn(m,c,k,x0,v0,t);
plot(t,y)
xlabel('time')
ylabel('x(t)')
grid on
hold on
function [y]=myfunn(m,c,k,x0,v0,t)
w0=sqrt(k/m);
zeta=c/(2*sqrt(k*m));
if(zeta>0 && zeta<1)
y=(exp(zeta.*(-1).*t)).*((x0.*cos(w0.*sqrt(1-zeta.^2).*t))+(v0+zeta.*w0.*x0).*sin(sqrt(1-zeta.^2).*t)./(w0.*sqrt(1-zeta.^2)));
elseif(zeta==1)
y=(exp(zeta.*(-1).*t)).*(x0+t.*(zeta.*w0.*x0));
elseif(zeta>1)
y=(exp(zeta.*(-1).*t)).* ...
(((v0+(zeta+sqrt(zeta.^2-1)))).*exp(w0.*sqrt(zeta.^2-1).*t)./2.*w0.*sqrt(zeta.^2-1) + ...
((-v0+(-zeta+sqrt(zeta^2-1))))*exp(-1*w0.*sqrt(zeta.^2-1).*t)./2.*w0.*sqrt(zeta.^2-1));
end
end
0 comentarios
Más respuestas (1)
Ver también
Categorías
Más información sobre Assembly en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!