Nyquist criterion - undefined variable in algorithm - matlab

I write an algorithm that checks the stability of the closed system of the Nyquist criterion (http://en.wikipedia.org/wiki/Nyquist_stability_criterion)
function answear=stability(re,im)
%% Function check stability of system
%re is real part of transmitation
%im is imagine part of transmitation
%% Check number of vectors elements
re(end +1:5) = 0;
im(end +1:5) = 0;
if( length(re) > length(im))
root = length(re);
else
root = length(im);
end
for w=1:root
tran(w) = re(1) + re(2)*w.^1 + re(3)*w.^2 + re(4)*w.^3 + re(5)*w.^4 +1i*(...
im(1) + im(2)*w.^1 + im(3)*w.^2 + im(4)*w.^3 +im(5)*w.^4);
end
%% Algorithm
switch root
case 0
exist('Write nonzero numbers', 'var')
case 1
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
answear=1;
else
answear=0;
end
end
case 2
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
answear=1;
else
answear=0;
end
end
end
case 3
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) < 0)
answear=1;
else
answear=0;
end
end
end
end
case 4
for w=1:length(w)
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) < 0)
if( real(tran(w)) > 0 && imag(tran(w)) < 0)
answear=1;
else
answear=0;
end
end
end
end
end
end
%% Answear
if answear==1
disp('System unstable')
else
disp('System stable')
end
plot(real(tran),imag(tran))
grid on
end
Function returns
Undefined function or variable "answear".
Error in stability (line 87) if answear==1
So the algorithm is badly written?

Your code could use a lot of cleanup:
Instead of an if-statement such as this one:
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
answear=1;
else
answear=0;
end
you could write a boolean assignment:
answear = real(tran(w)) > 0 & imag(tran(w)) > 0);
Why do you have three (almost) identical nested if-statements at all?
if( real(tran(w)) > 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) > 0)
if( real(tran(w)) < 0 && imag(tran(w)) < 0)
First of all, you could replace everything with one if-statement. But what are you actually testing with this? It seems that those nested if statements are never executed. For instance, real(tran(w)) cannot be both positive and negative at the same time (unless it is a vector you are working on, in which case you shouldn't be using the operator &&).
Also, this is probably your code triggers the error regarding variable answear. Accessing it is impossible since it hasn't been assigned a value (none of the if-statements have been executed).
What is tran, and what is w? Are they global variables? If they are, pass them as input parameters. Your function is probably poorly designed if it relies on external states and variables.
I haven't actually run your code, but these suggestions should make it easier for you to debug it.
P.S:
Please fix the annoying spelling error (it is "answer" and not "answear") :)

Related

Test for scalar function inputs is failing

Despite using isscalar, isinteger... the result after running is still true, I still had this test which is incorrect and in relation with Scalar values.
My program:
function valid = valid_date(year, month, day)
[y1 y2] = size(year);
[m1 m2] = size(month);
[d1 d2] = size(day);
if (mod(year,4)==0 && mod(year,100)~=0) || mod(year,400)==0
is_leap_year = 1;
else
is_leap_year = 0;
end
if ~(y1==y2==d1==d2==m1==m2==1)
valid = false;
elseif any(month == [1, 3, 5, 7, 8, 10, 12])
valid = (day >0 && day <= 31)
elseif any(month == [4, 6, 9, 11])
valid = day >0 && day <= 30;
elseif month == 2 && is_leap_year == 1
valid = (day >0 && day <=29);
elseif month == 2 && is_leap_year == 0
valid = (day >0 && day <=28);
else
valid = false;
end
end
The result after submitting my program, all tests are passed except the one related to scalar values:
Why did my program fail on the non-scalar test?
The way you're checking for scalars is really not well defined.
~(y1==y2==d1==d2==m1==m2==1)
This chained equivalence check is not the same as checking if all of your variables are equal to 1, consider the following counter-example:
1==0==0==1 % Output: 1
In this case none of your comparison variables should be 0, so you might skirt this issue, but it's still best to avoid it. You're also contending with floating point comparisons which are prone to issues.
You should use isscalar, you say you tried it but didn't show how. Something like this should work:
function valid = valid_date(year, month, day)
if ~( isscalar(year) && isscalar(month) && isscalar(day) )
valid = false;
return
end
% ... other code now we know inputs are scalar
end
Thank you so much #Wolfie. The problem is solved.
I used what you told me about and put it at the beginning of my function.
I show you guys the code, in case you had the same error:
function valid = valid_date(year, month, day)
if ~(isscalar(year) && isscalar(month) && isscalar(day))
valid = false;
return
end
if ((mod(year,4)==0 && mod(year,100)~=0) || mod(year,400)==0)
is_leap_year = 1;
else
is_leap_year = 0;
end
if any(month == [1, 3, 5, 7, 8, 10, 12])
valid = (day >0 && day <= 31);
elseif any(month == [4, 6, 9, 11])
valid = day >0 && day <= 30;
elseif (month == 2 && is_leap_year == 1)
valid = (day >0 && day <=29);
elseif (month == 2 && is_leap_year == 0)
valid = (day >0 && day <=28);
else
valid = false;
end
end
Yaaay all the tests are passed lhamdullah!

Last elseif statement does not execute

My last elseif statement does not execute even if the conditions are met:
Currency_Exchanage != 'Select...' and all other variables (ETF_Exchanage, Index_Exchanage and Stock_Exchanage) = 'Select...'
Here is the section of code that I am concerned about:
if (strcmp(ETF_Exchanage,'Select...') == 1) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1)
if db == 1 && uni == 1
tickers = gnr_bloomberg; % Analsise Bloomberg natural resources
nrm=1;
elseif db == 1 && uni == 2
tickers = all_bloomberg; % Analsise Bloomberg all
nrm=1;
elseif db == 2 && uni == 1
tickers = gnr_yahoo; % Analsise Yahoo natural resources
nrm=1;
elseif db == 2 && uni == 2
tickers = all_yahoo; % Analsise Yahoo all
nrm=1;
end
else
%Yahoo inputs
if (strcmp(ETF_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from ETF
tickers = ETF_Yahoo(:,1);
Exchanges = ETF_Yahoo(:,2);
Exchange = ETF_Exchanage;
db=2; %Yahoo Selection
elseif (strcmp(Index_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(ETF_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from Index
tickers = Index_Yahoo(:,1);
Exchanges = Index_Yahoo(:,2);
Exchange = Index_Exchanage;
db=2;
elseif (strcmp(Stock_Exchanage,'Select...') == 0) && (strcmp(ETF_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from Stock
tickers = Stock_Yahoo(:,1);
Exchanges = Stock_Yahoo(:,2);
Exchange = Stock_Exchanage;
db=2;
elseif (strcmp(Currency_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(ETF_Exchanage,'Select...') == 1); %Choose exhanges from Currency
tickers = Currency_Yahoo(:,1);
Exchanges = Currency_Yahoo(:,2);
Exchange = Currency_Exchanage;
db=2;
else
msg = 'Error occurred.\Only one Yahoo input menue must be used!';
error(msg)
end
end
Any Help would be much appropriated, I can't see where I'm going wrong here. I am using Matlab 2013a.
Put a breakpoint at the elseif statement in question and then check in the command window what your condition evaluates to.
If it does not evaluate like expected, check what the individual terms evaluate to.
It is important to actually test what the conditions evaluate to in matlab, rather than only visually comparing the string values.
Usually by that point you should get a rough idea what is wrong.
However in your case we can't do these steps for you because something is off. Your code condensed to the more reasonable minimal example
if 1 && 1 && 1...
1;
disp('I was here')
end
does not even execute in R2014a since the interpreter complains about '...' being an unexpected matlab expression.

Arithmetically simulate 32-bit integer overflow

Is there a way to arithmetically simulate 32-bit, twos-complement integer overflow with numbers of a type whose value space is a strict superset of that of the 32-bit twos-complement integers? I need to perform such an operation in, for example, WolframAlpha, which has no explicit typing and no type casting.
The output of my desired convertTo32BitSignedInt(aValue) function needs to be the same as if I had cast the value in a language that supports it, such as Java: (int)aValue.
By example, the output of convertTo32BitSignedInt(17643225600) needs to be 463356416, the same as I had used the following cast (int)17643225600.
For the moment, my convertTo32BitSignedInt function (in pseudo code) looks like this and I am pretty sure it's not the better solution.
if(x > 2147483647 && floor(x / 2147483647 ) == 1){
return (x - 2*2147483648);
}else if(x > 2147483647 && (x % 2147483647 == 0) && (x % 2 == 0)){
return -1 * (x / 2147483647);
}else if(x > 2147483647 && (x % 2147483647 == 0) && (x % 2 > 0)){
return -1 * ((x - 2147483647) / 2147483647) + 2147483647;
}else if(x > 2147483647 && (x % 2147483647 > 0) && (x % 2 == 0)){
return -1 * floor(x / 2147483647) + (x % 2147483647);
}
//...
Use Case:
I try to demonstrate a certain behavior that will occures when there is a 32-bit signed integer overflow in a java program using a recursive implementation of the factorial function.
public int factorial(int n) {
return n == 1 || n==0 ? 1 : n * factorial(n - 1);
}
This int implementation, for factorial(13) gives 1932053504, because 13 * 479001600 > 2147483647, the 32-bit signed integer maximum value.
I use WolframAlpha for the demonstration. WolframAlpha allows numbers > 2147483647 and I want to simulate these number as 32-bit integer numbers. WolframAlpha gives the real answer which is 6227020800. I would like to be able to convert 6227020800 as 1932053504.
Edit: this is a rip & replace of my initial answer
You can do something roughly like this:
convertTo32BitSignedInt(aValue) {
bits32 = BitAnd(aValue, BitShiftLeft(1, 32) - 1);
sign = BitShiftRight(bits32, 31);
return bits32 - BitShiftLeft(sign, 32);
}

Testing Logic with More than 1 && Operator?

If I have a function with 2 inputs, is there a way to test both its range other than nested if functions?
For example...
if input1 > 0 && input1 <10 && input2 > 0 && input2 <10
this doesn't work so I'm forced to write
if input1 > 0 && input1 <10
if input2 > 0 && input2 <10
'%something
end
end
Are there easier ways to test range of multiple inputs?
I don't know of a way to avoid nested if functions, but you can avoid repetition:
In JavaScript:
function test(argument1,argument2) {
check = 0;
for (i=0;i<arguments.length;i++){
if (arguments[i] > 0 && (arguments[i] < 10)) {
check++;
if (check === arguments.length) {
//do something
}
}
}
}
test(1,2);
Try this in Matlab:
find((input1==1:20),1) & find((input2==50:100),1)

unexpected results of a function in matlab

Normally this function should give me the values ​​1, 2, 3 or 4. but when I use it, I get 0, 1 or 2. Could you help me to know where is the problem:
function Vecteur_retour = var_Test(Test)
AA = Test;
var_Test = zeros(1,2000);
for i=3:1:2000
if AA(i)<=AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=1;
else
if AA(i)<=AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=2;
if AA(i)>AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=3;
else
if AA(i)>AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=4;
end
end
end
end
end
Vecteur_retour = var_Test;
Vector comparisons will be much faster:
var_Test = ones(1,2000);
delta_Test = diff(Test);
var_Test([0 0 delta_Test(1:end-1)] > 0) = 2;
var_Test([0 delta_Test] > 0) = var_Test([0 delta_Test] > 0) + 2;
var_Test(1:2) = 0;
Probably because you never reach the cases var_Test(i) = 3 or var_Test(i) = 4.
You have a problem with your if and end blocks. The way you have it, case 3 is only reached if case 2 is hit first, but these are contradictory.
You want code more like.
function Vecteur_retour = var_Test(Test)
AA = Test;
var_Test = zeros(1,2000);
for i=3:1:2000
if AA(i)<=AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=1;
else
if AA(i)<=AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=2;
else % you forgot this else
if AA(i)>AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=3;
else
if AA(i)>AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=4;
end
end
end
end
end
Vecteur_retour = var_Test;
Careful indentation would have helped here.