0

I'm trying to plot a function on matlab that's defined such that: Y=-100t ; 0>=t>=0.15 Y=-15 ; t>.15

I'm using the following code:

function [ Y ] = Gain( t )

for t=[0:0.01:0.15]

        Y=-100*t
end

for t=0.15:0.01:2
        Y=-15
end
plot (Gain)

but I'm going into an infinite loop!

Would someone please solve this problem for me.

Thank you.

5
  • Why are you setting Y to -15 in a loop? Also, what are you expecting to hapen when you loop t from 0.15 to 2, with the step of 1? That doesn't seem right. Commented Mar 24, 2014 at 0:54
  • Because I want to draw a plot that's equal to -15 between 0.15 and 2. and changing the loop step to 0.01 doesn't help anyways I've tried that. Commented Mar 24, 2014 at 1:10
  • Why is t a parameter? Commented Mar 24, 2014 at 1:11
  • @user3140090 Obviously, however it's not an input to the function, which promptly clobbers it as a variable. Commented Mar 24, 2014 at 4:52
  • Since you are already changing value inside the function, you are better off writing a MATLAB script instead of the function. You can do something like this - Y=[-100.*(0:0.01:0.15) -15+0.*(0.15:0.01:2)]; plot(Y). Commented Mar 24, 2014 at 6:08

2 Answers 2

1

Functions don't work like that in MATLAB, unfortunately. (Or at least I don't think they do). Try something like this:

function Y = Gain(t)
    Y = -100*t;
    Y(t >= 0.15) = -15;
end

x = 0:0.01:2;
plot(x, Gain(x))

MATLAB still uses C-esque functions, so you have to define it that way, using C-style syntax, instead of more math-like syntax, unfortunately. I'm multiplying the input values by -100, then for the ones that match up to where t is greater than 15, I replace those with -15. MATLAB is weird.

Edit: Sorry, previous code sample also used the wrong syntax.. MATLAB is weird.

Sign up to request clarification or add additional context in comments.

2 Comments

what sort of variable t should be ? I tried Global but it's not working. I think this is why I'm having the error: Operands to the || and && operators must be convertible to logical scalar values.
Try the new code I posted. Sorry, it's been a while since I've used MATLAB.. Here it's assumed t's either some sort of vector, matrix, or set.
1

I'm not certain what you're TRYING to do, but when you call the Gain function "from outside", so to speak,...

  1. You enter the Gain function
  2. The first for loop executes, overwriting passed-in value of t with each iteration. (Therefore, the value of t that you passed in is completely ignored, the way that you have this code written.)
  3. The second for-loop executes, and
  4. You call plot(Gain), which forces Gain to be called again, this time with no arguments. Back to 1.

Repeat forever.

1 Comment

+1, it sounds like the problem is a missing end before the last line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.