issues with if statement and plotting values

11 views (last 30 days)
Mike
Mike on 23 Jun 2012
I am a complete novice with the use of matlab and would like some advice.
I am trying to set up a list of numbers called 't' and a separate value named 'td'. I would then check whether each element within the list satisfies the condition t < td. If this condition is true, I would like to carryout the mathematical operation called 'op1' and if otherwise, carryout 'op2'. I would then like to plot values the operations against their corresponding values of 't'.
What I have tried so far is displayed below but does not work. Can someone point me in the correct direction and explain where I've been going wrong.
Omega = 183.3;
td = 0.5;
t = 0:0.1:1;
op1 = (P/stiffness)* ((1-cos(Omega*t)) + (sin(Omega*t)/(Omega*td)) - (t/td));
op2 = (P/stiffness)* ((1/(Omega*td)) * (sin(Omega*t) - sin(Omega*(t-td))) - cos(Omega*t));
y = zeros(1,t);
if t < td
y(t) = op1;
else
y(t) = op2;
end
plot(y,t) ;

Answers (3)

Luffy
Luffy on 23 Jun 2012
I did not exactly understand what you r trying to do,but here's my advice:-
  • did you declare the variables P,stiffness in your code?
  • You cannot write zeros(1,t) where t(0:0.1:1) is not a positive integer.
  • You also cannot write y(t), as in matlab indices are positive integers,you should start from y(1),in your code in the beginning itself it refers to y(0).
  1 Comment
Mike
Mike on 23 Jun 2012
Thank you for your response, but as I am not yet up to speed with matlab, I am struggling to understand your advice. I did declare the variables P and Stiffness a priori and I will try to explain again. Maybe you can help me to formulate a better way for me to achieve my goal.
Step one: create a vector called 't' which has values as specified above (0:0.1:1)
Step two: take each element of the vector 't' in turn and follow the process below:
t < td
if so, then substitute t value in op1 and if not, substitute into op2
store all values of op1 and op2 in a vector called 'y'
for example when t = 0;
check whether t = 0 is less than a predefined value of td. (td = 0.5 in this case)
As this would be true, then evaluate 'op1' placing the value of t =0 wherever 't' appears in the expression and store it to a vector called 'y'.
When this is not true as would be the case for t = 0.5 and above, I would like the code to evaluate these t values using 'op2' and then store it to the same vector 'y'.
Therefore the vector y should be the same size as the vector t.
Step three: plot the y values obtained against corresponding t values.

Sign in to comment.


Luffy
Luffy on 23 Jun 2012
I understand your code now,I took P & stiffness as 1 and wrote code,so you might add them to this code otherwise, I think this should help.
Omega = 183.3;
td = 0.5;
t = 0:0.1:1;
op1 = ((1-cos(Omega*t)) + (sin(Omega*t)/(Omega*td)) - (t/td)); % add P & stifness I put them to 1.
op2 = ((1/(Omega*td)) * (sin(Omega*t) - sin(Omega*(t-td))) - cos(Omega*t));
if t < td
y = op1;
else
y = op2;
end
plot(y,t) ;
plot(t,y) ; % this gave a better looking graph as in writing plot(t,y) plots graph with values of t on x-axis and y on y-axis.
  1 Comment
Mike
Mike on 23 Jun 2012
Edited: Walter Roberson on 15 Jul 2012
Monkey D. Luffy, I really appreciate your help but the new code you have provided is not giving the correct values for 'y' although the plot has the correct shape. I really do not understand why this is, because if I type the following in the command window:
t = 0;
op1 = ((1-cos(Omega*t)) + (sin(Omega*t)/(Omega*td)) - (t/td));
op1 = 0
therefore for t=0, y should also equal 0;
however this is not the case within the plot, a negative value is attained. I have found a solution to the initial problem, which does not use the IF statement (see below):
op1 = (P/stiffness)* ((1-cos(Omega*t)) + (sin(Omega*t)/(Omega*td)) - (t/td));
op2 = (P/stiffness)* ((1/(Omega*td)) * (sin(Omega*t) - sin(Omega*(t-td))) - cos(Omega*t));
y = op1 .* (t < td);
y = y + op2 .* (t >= td);
plot(t,y);
however, I wonder whether this can be achieved with the if code you provided as it is so close to the value required. In the editor, there is a red squiggly line under t in the line (if t < td), if I hover over 't', the error message is given 'Variable t might be set by a nonscalar operator'... Any ideas how to overcome this?

Sign in to comment.


Walter Roberson
Walter Roberson on 15 Jul 2012
Notice this part of the if documentation:
An evaluated expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false.
Your t is set to a row vector, t = 0:0.1:1, so when you compare to the scalar 0.5, some of t < 0.5 are true and some are false, and since not all the values are true, the "if" will be deemed to be false.
When you program an "if", mentally thing of there being an "all()" around the expression. If the result clearly will not work in context then you have programmed the wrong "if" and perhaps need to program a loop. Or, better yet, rewrite in terms of logical indexing.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!