Angles in Sine/Cosine swap

Hi folks, Using MATLAB 2011b. I have a code in which I have equations with symbolic variables. Now, when I am setting up a sin or cosine function, it keeps switching the two angles. How do I stop that from happening? Here is an example:
What I want: y=sin(del_5-del_3)
What MATLAB stores: y=sin(del_3-del_5)
It is important that the two angles remain where they are so I am not sure what I can do to fix it. Any help will be much appreciated!
Code (Please note that I am still working on it so its incomplete). The sines and cosines can be seen in the "Equations Setup" section with the for loops
clear all
clc
%%Initialization
% Bus Voltages and angles
V1=1.03;
V2=1.03;
del_V1=0;
% Transformer 4 changes
T4_init = (0.01+0.11i)*25/36;
t=12.9/13.8;
T4=(1/T4_init)*t;
s79=(1/T4_init)*(t^2-t);
s97=(1/T4_init)*(1-t);
% Impedance Table
L1=0.008+0.08i;
L2=0.005+0.05i;
L3=0.008+0.08i;
L4=0.005+0.04i;
L5=0.005+0.05i;
L6=0.005+0.5i;
T1=(0.01+0.09i)/5;
T2=(0.006+0.06i)/5;
T3=(0.01+0.11i)*25/36;
L=[0 1/L2 1/L1 0 0 0 0 0 0;
1/L2 0 0 1/L3 0 0 0 0 0;
1/L1 0 0 1/L4 1/T1 0 0 0 0;
0 1/L3 1/L4 0 0 0 0 1/T2 0;
0 0 1/T1 0 0 1/L5 0 0 0;
0 0 0 0 1/L5 0 1/T3 0 0;
0 0 0 0 0 1/T3 0 0 T4;
0 0 0 1/T2 0 0 0 0 1/L6;
0 0 0 0 0 0 T4 1/L6 0];
S=[0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 s79;
0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 s97 0 0];
% Load Conditions
LoadA_4 = 400-100i;
LoadA_7 = 60+10i;
LoadA_9 = 50+20i;
LoadB_4 = 500-200i;
LoadB_7 = 80+10i;
LoadB_9 = 100+20i;
LoadC_4 = 600-200i;
LoadC_7 = 100+10i;
LoadC_9 = 200-20i;
if true
% code
end
% Unknowns
syms V3; syms V4; syms V5; syms V6;
syms V7; syms V8; syms V9;
syms del_V2; syms del_V3; syms del_V4; syms del_V5;
syms del_V6; syms del_V7; syms del_V8; syms del_V9;
% Other Setup variables
syms V
syms del
syms g_P
V=[V1;V2;V3;V4;V5;V6;V7;V8;V9];
del=[del_V1;del_V2;del_V3;del_V4;del_V5;del_V6;del_V7;del_V8;del_V9];
%%Equations Setup
for i=1:1:9
for j=1:1:9
g_P(i,j)=(real(L(i,j))*V(i)^2)-(real(L(i,j))*V(i)*V(j)*cos(del(i)-del(j)))+(-imag(L(i,j))*V(i)*V(j)*sin(del(i)-del(j)));
g_Q(i,j)=-(imag(S(i,j))*V(i)^2)+(-imag(L(i,j))*V(i)^2)-(real(L(i,j))*V(i)*V(j)*sin(del(i)-del(j)))-(-imag(L(i,j))*V(i)*V(j)*cos(del(i)-del(j)));
end
end
g(1,1)=4.5-g_P(2,1)-g_P(2,4); % Node 2 Real
g(2,1)=g_P(3,1)+g_P(3,4)+g_P(3,5); % Node 3 Real
g(3,1)=g_P(4,2)+g_P(4,3)+g_P(4,8); % Node 4 Real
g(4,1)=g_P(5,3)+g_P(5,5); % Node 5 Real
g(5,1)=g_P(6,5)+g_P(6,7); % Node 6 Real
g(6,1)=g_P(7,6)+g_P(7,9); % Node 7 Real
g(7,1)=g_P(8,4)+g_P(8,9); % Node 8 Real
g(8,1)=g_P(9,8)+g_P(9,7); % Node 9 Real
g(9,1)=g_P(3,1)+g_P(3,4)+g_P(3,5); % Node 3 Imaginary
g(10,1)=g_P(4,2)+g_P(4,3)+g_P(4,8); % Node 4 Imaginary
g(11,1)=g_P(5,3)+g_P(5,5); % Node 5 Imaginary
g(12,1)=g_P(6,5)+g_P(6,7); % Node 6 Imaginary
g(13,1)=g_P(7,6)+g_P(7,9); % Node 7 Imaginary
g(14,1)=g_P(8,4)+g_P(8,9); % Node 8 Imaginary
g(15,1)=g_P(9,8)+g_P(9,7); % Node 9 Imaginary
%%Solution

1 Comment

Muazzam, please post the entire code for us to troubleshoot.

Sign in to comment.

Answers (1)

Here is your code:
g_Q(i,j)=-(imag(S(i,j))*V(i)^2)+(-imag(L(i,j))*V(i)^2)-(real(L(i,j))*V(i)*V(j)*sin(del(i)-del(j)))-(-imag(L(i,j))*V(i)*V(j)*cos(del(i)-del(j)));
I see that it does the subtraction of the del array elements twice in that equation: del(i)-del(j). You say you want to add them instead. So just change your equation so that it uses del(i)+del(j):
g_Q(i,j)=-(imag(S(i,j))*V(i)^2)+(-imag(L(i,j))*V(i)^2)-(real(L(i,j))*V(i)*V(j)*sin(del(i)+del(j)))-(-imag(L(i,j))*V(i)*V(j)*cos(del(i)+del(j)));
Do the same thing for the g_P equation.

6 Comments

Muazzam
Muazzam on 30 Nov 2014
Edited: Muazzam on 30 Nov 2014
Sorry thats a typo on my part. it is supposed to be negative in the first equation: What I want: y=sin(del_5-del_3)
What MATLAB stores: y=sin(del_3-del_5)**
I don't see a y, or a del_3, or a del_5 in the code. I see del(i), del(j), and a whole bunch of things like del_V1, del_V2, etc.
Muazzam
Muazzam on 30 Nov 2014
Edited: Muazzam on 30 Nov 2014
Yes, saying del_3 del_5 was just an example. When the code runs, del(3) = del_V3 and all. These are just naming conventions.
cos(del(i)-del(j)));
What is happening is that when this cos (or any sine) is processed, sometimes it switches del(i) with del(j) which I don't want it to do!
How do you know? Because cos(del(i)-del(j))) is the very same value as cos(del(j)-del(i))) because cosine is an even function. Isn't cos(.1) the same as cos(-.1)? Of course it is. Why don't you break your gigantic expression into smaller terms and check each of the terms individually to find out where your error is creeping in?
I know this because after I have run the program, I can see them switched in g_P and g_Q matrix where I am storing it. When taking the derivatives, switching the angles will mean that the sign will change.
Do you know, just theoretically, why MATLAB would do that? Why does it actually switch the angles?
It doesn't . There is a problem in your code somewhere and you will discover it by breaking the equation down into simpler terms and inspecting them, like I suggested. Did you do that yet? Learn how to do that here.

Sign in to comment.

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Asked:

on 29 Nov 2014

Commented:

on 30 Nov 2014

Community Treasure Hunt

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

Start Hunting!