Plot an equation by calling a function

3 ビュー (過去 30 日間)
Fatemeh Salar
Fatemeh Salar 2022 年 7 月 9 日
コメント済み: Rik 2022 年 7 月 11 日
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 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
And 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
Maybe I am wrong in calling a function ask ask it to plot .... Idk
  3 件のコメント
Fatemeh Salar
Fatemeh Salar 2022 年 7 月 10 日
@Walter Roberson I changed the entire question and now wanted to delete it . I post it again here , so I flagged this question to get deleted (apperantly I can't delete it by myself)
Thanks.
Rik
Rik 2022 年 7 月 11 日
You received an answer. That is why you can no longer delete it. Materially editing your question will only make it confusing for future readers with a similar question.
Asking a second question is not a problem, but why mutilate the first question in order to do so?

サインインしてコメントする。

回答 (1 件)

KSSV
KSSV 2022 年 7 月 9 日
It looks like your e in the function is exp. Change the function to:
function [y]=myfun(a,b,c,t)
if (a*b/c>1)
y = (exp(a))*sin(c.*t)./sqrt(b);
elseif (a*b/c<1 && a*b/c>0)
y = sin(a)*sqrt(b.*t)./sin(c);
elseif (a*b/c < 1)
y = cos(a)*exp(b.*t)./c;
end
end
If e is not exp, then you need to define e like a,b,c and input into the function.
  3 件のコメント
KSSV
KSSV 2022 年 7 月 9 日
Use the function which I gave. It works.
Fatemeh Salar
Fatemeh Salar 2022 年 7 月 9 日
編集済み: Fatemeh Salar 2022 年 7 月 9 日
My bad! I think I asked a question I already knew its answer lol
The thing is I made a mistake at the first place. There was an error for another function of mine not that one you saw earlier. So instead of deleting my previous question I edited it.
The proplem is with my function [y]=myfunn(m,c,k,x0,v0,t)
And the picture was the result of this function that ain't working :S

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAssembly についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by