필터 지우기
필터 지우기

how to write an equation with different period of time

조회 수: 6 (최근 30일)
Abdulelah
Abdulelah 2014년 2월 3일
편집: per isakson 2017년 9월 17일
how can I writ this equation in matlab.
25*t 0<= t <= 2
W(t)= 50 2<= t <= 4
-05t+250 4<= t <=5
thank for everyone trying to help.

답변 (3개)

Tomas
Tomas 2014년 2월 3일
편집: Tomas 2014년 2월 3일
I can think of two ways, one is using a "for" and ifs and its not very efficient but it will get it done like this:
t=0:0.1:5
for i=1:length(t)
if t(i)>=0 &&t(i)<=2
w(i)=25*t;
elseif t>=2 && t<=4
w(i)=50;
elseif t>=5 && t<=5
w(i)=-5*t+250;
end
end
The other one is harder to explain. I hope this is enough.
  댓글 수: 1
Abdulelah
Abdulelah 2014년 2월 3일
thank you unfortunately it doesn't work with me.
I got this message. In an assignment A(I) = B, the number of elements in B and I must be the same.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2014년 2월 3일
w = nan(size(t));
idx = 0 <= t & t= < 2;
w(idx) = t(idx) * 25;
idx = 2 <= t & t <= 4;
w(idx) = 50;
idx = 4 <= t & t <= 5;
w(idx) = 250 - 50 * t;
Notice that this leaves undefined (NaN) any position for which t < 0 or t > 5.
You double-define the values at 2 exactly and 4 exactly but the two definitions have the same value at the exact boundary so there is no discontinuity.
  댓글 수: 4
Abdulelah
Abdulelah 2014년 2월 4일
first I got this
EDU>> w = nan(size(t));
Undefined function or variable 't'.
second error is
EDU>> idx = 0 <= t & t= <2;
idx = 0 <= t & t= <2;
Error: The expression to the left of the equals sign is not a valid target for an assignment.
Walter Roberson
Walter Roberson 2014년 2월 4일
편집: per isakson 2017년 9월 17일
function r = w(t)
r = nan(size(t));
idx = 0 <= t & t= < 2;
r(idx) = t(idx) * 25;
idx = 2 <= t & t <= 4;
r(idx) = 50;
idx = 4 <= t & t <= 5;
r(idx) = 250 - 50 * t;
end
The above defines an equation named "w" that takes a parameter on input and returns the value according to your definition.
This is what you asked, to define the equation.
You can use the equation like any MATLAB function, by calling its name and passing in the value you want to process. For example,
T = -10:.01:10;
plot(T, w(T))

댓글을 달려면 로그인하십시오.


Amit
Amit 2014년 2월 3일
w_t = [25*T 50 250-50*t];
t_range = [0 2 4];
W = w_t(find(t > t_range,1,'last'));
  댓글 수: 1
Abdulelah
Abdulelah 2014년 2월 4일
Thank you, but I got this message
EDU>> t = 0:0.1:5;
EDU>> w_t= [25*t 50 250-50*t];
EDU>> t_range = [0 2 4];
EDU>> W = w_t(find(t > t_range,'last'));
Error using >
Matrix dimensions must agree.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by