switching functions for continuous time

I am plotting a function for say:
t = 0:0.1:1 %seconds
I want to use one function for:
t < 0.2
One for:
t >= 0.2 & t <= 0.0.8
And then one for:
t >0.8
I can't get it to work using the conventions I have stated aboce

 채택된 답변

Walter Roberson
Walter Roberson 2011년 5월 25일

0 개 추천

y = zeros(size(t));
idx = t<0.2;
y(idx) = f1(t(idx));
idx = t>=0.2 & t<=0.08
y(idx) = f2(t(idx));
idx = t>0.8;
y(idx) = f3(t(idx));

추가 답변 (8개)

A
A 2011년 6월 6일

0 개 추천

The problem is I want to plot them all on one graph.
So I need to piece together the part from 0-0.2, 0.2-0.8, and 0.8-1.10 seconds.
EDIT - also do I just replace f1, f2, f3 with my function?

댓글 수: 3

Walter Roberson
Walter Roberson 2011년 6월 6일
Yes, just replace f1, f2, f3 with your functions. The above code already pieces the parts together.
A
A 2011년 6월 7일
so how would i plot it then?
Walter Roberson
Walter Roberson 2011년 6월 7일
plot(t,y)

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

A
A 2011년 6월 14일

0 개 추천

How can I apply the same concept, but switch functions based on the value of the function?
Say y is a step function going from 0 to 100.
I want to switch from one function to another at y = 80, instead of using time.

댓글 수: 2

Walter Roberson
Walter Roberson 2011년 6월 14일
"for" loop and an if/elseif structure if it is expensive to compute the values.
If computing the values is relatively cheap,
y = f1(x);
idx = find(y >= 80,1,'first');
y(idx:end) = f2(x(idx:end));
A
A 2011년 6월 15일
You're awesome Walt.

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

A
A 2011년 6월 21일

0 개 추천

If my function is:
y = f1(x);
How can I select an x value for a given y?
This is a silly question I know, but I cannot figure it out.

댓글 수: 5

Walter Roberson
Walter Roberson 2011년 6월 21일
There can be multiple x of equal weight.
ydiff = abs(y - SpecificY);
closestX = x(ydiff == min(ydiff)); %warning, can have multiple entries
You would need different logic if, for example, you wanted only x for which y did not exceed the specific Y; the above logic finds the x for which the y is numerically closest above _or_ below the specific Y.
A
A 2011년 6월 21일
Yes I need to find x for which y does not exceed Y (specific y).
A
A 2011년 6월 21일
But the closest to it as possible.
A
A 2011년 6월 21일
I implemented the above, but I had to use the index (like you showed in your previous answers as 'idx' instead of x.
Then I used that to find the time.
This works well for now, but ideally I want to get to the more rigorous solution.
Thanks Walt, let me know about the rest when you get a chance.
Walter Roberson
Walter Roberson 2011년 6월 21일
Good point about the index; sorry about that.
If your y are in increasing order, then
find(SpecificY < y,1,'last')

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

A
A 2011년 6월 23일

0 개 추천

Now I am trying to find the time constant for this system and I need to do it analytically.
So I know the time constant is just 63.2% of the final value...
V_In is the step input (100 V) V_C_SS is the function that is going from 0V to 100V
Time_Value = V_In * .632; %Find the value that is 63.2%
idxx = find(V_C_SS <= Time_Value); %Create index
Time_Diff = abs(V_C_SS - Time_Value);
Time_Check = idxx(Time_Diff == min(Time_Diff));
Time_Constant = t(idxx)
Doesn't work. I tried to apply what you have showed me here. I just need to know the time it takes for the function to reach 63.2% of the final value.

댓글 수: 3

A
A 2011년 6월 23일
The error is coming on this line:
Time_Check = idxx(Time_Diff == min(Time_Diff));
??? Index exceeds matrix dimensions.
Walter Roberson
Walter Roberson 2011년 6월 23일
Time_Value = V_In * .632;
Time_Check = find(V_C_SS <= Time_Value,1,'last');
Time_Constant = t(Time_Check);
A
A 2011년 6월 23일
Ok I see now. Thank you.

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

A
A 2011년 6월 23일

0 개 추천

On the switching case here:
y = f1(x);
idx = find(y >= 80,1,'first');
y(idx:end) = f2(x(idx:end));
How can I stop f1 and only have f2 start where f1 ends? Using the final values of f1 for IC's of f2.

댓글 수: 4

Walter Roberson
Walter Roberson 2011년 6월 23일
You haven't really defined what it means for f1 to "end", or what it means to use the final values of f1 for the initial conditions of f2. So, guessing...
overlap = 5; %samples
y = f1(x);
idx = find(y >= 80,1,'first');
startat = max(1,idx-overlap);
f2vals = f2(x(startat:end));
y(idx:end) = f2vals(idx-startat+1:end);
A
A 2011년 6월 23일
I mean for f1 to terminate at the point where, y=80, and f2 to begin using that (t,y) as its first point.
Walter Roberson
Walter Roberson 2011년 6월 23일
That's what the code you posted above does, unless f2 is sensitive to the position of the x as well as to the value of the x. If it _is_ sensitive to the position of the x, then the next thing we would need to know is whether the f2 values are calculated independently or if values from earlier input influence later output.
If there is dependence on the position then,
y = f1(x);
y2 = f2(x);
idx = find(y >= 80,1,'first');
y(idx:end) = y2(idx:end);
A
A 2011년 6월 23일
Ok yes this works great, thank you.

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

A
A 2011년 6월 23일

0 개 추천

Referring to the time constant again:
Time_Value = V_In * .632;
Time_Check = find(V_C_SS <= Time_Value,1,'last');
Time_Constant = t(Time_Check);
My function is sinusoid and oscillates...
So my function drops below 63.2% (Time_Value) more than once. How can I select only the first time it goes to this value?

댓글 수: 1

Walter Roberson
Walter Roberson 2011년 6월 23일
Time_Check = find(V_C_SS > Time_Value,1,'first') - 1;
Provided that your values do not start out above the time constant, dip, rise, with it being the point on the rise you want to get.

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

A
A 2011년 6월 23일

0 개 추천

Ah I see. You are great with the indices... thank you Walt.
A
A 2011년 6월 24일

0 개 추천

Using only one function, I want to 'track' an input (voltage) schedule.
I.E. I have made a voltage schedule that I read in... it has a time vector and a voltage vector.
The voltage changes in steps. It starts at 0 and then will jump to 80V, stay for a few milliseconds and then drop down to 25V, then back up to 60V, etc.
I am basically simulating a response (say a DC motor), and I want to see how well I can track this performance using my function.
What is the best way to go about this?

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

질문:

A
A
2011년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by