Monday, May 24, 2010

Eigenvectors calculation (from Numerical Recipes in C book)?

Hi,


I implemented the algorithms tred2() (page 474) and tqli() (page 480) for calculating eigenvectors and eigenvalues of real matrices, and comparing the results to the ones obtained with MatLab, some of the eigenvectors have the opposite sign. Every single value is ok, but the vector seems to be multiplied by -1.





Does anyone know why is this, and if this is actually an error?





Thanks in advance.





PS: the book can be found online at





http://www.nr.com/nronline_switcher.php

Eigenvectors calculation (from Numerical Recipes in C book)?
Eigenvectors are not unique. If your matrix has an eigenvector of





[ 1, -2]'





for example, then any multiple of that eigenvector is also an eigenvector for that hypothetical matrix, so





[2, -4]'


[50, -100]'


[-1, 2]'


[-1000, 2000]'





are all eigenvectors for this hypothetical matrix. That is because for any square matrix A with eigenvector x corresponding to eigenvalue λ and for any constant c





A(c*x) = c*Ax


= c*λx


= λ(c*x)





Which means that c*x is an eigenvector for A.





So, there is no error. Some are just multiplied by -1. You're good.





edit: No, the eigenvalues do not change. Note in my "proof" above that the same eigenvalue λ was used for the vectors x and c*x. Like I said, eigenvectors aren't unique; not even for a particular eigenvalue.





Here's an example.





A =


[ 4 -2 ]


[ 1 1]





x =


[ 2 ]


[ 1 ]





Ax =


[ 6 ]


[ 3 ]


= 3x





So x is an eigenvector of A with an eigenvalue of 3. Now let's look at y = -x = [-2 -1]'.





Ay =


[ -6 ]


[ -3 ]


= 3y





So y = -x is also an eigenvector of A with an eigenvalue of 3.





I can't message you, so send me a message if you have any questions.


AS Basic as it can get C++ Hangman?

i need help in hangman only using %26lt;iostream%26gt;, %26lt;lvp\string%26gt;, %26lt;lvp\vector%26gt; or bool..

AS Basic as it can get C++ Hangman?
wow, thats a great question... lets see... my answer is... around the corner


Can anyone write this code for c++?

Write a function that computes the alternating sum of all elements in a


alternating_sum is called with a vector containing


1 4 9 16 9 7 4 9 11


Then it computes


1 – 4 + 9 – 16 + 9 – 7 + 4 – 9 + 11 = -2

Can anyone write this code for c++?
Take a look at here:





http://www.msoe.edu/eecs/cese/resources/...


Finding the mode of an array C++?

I have a vector that records the number of occurances based on user input, but I cannot figure out how to find the mode of that data here is my code listing the occurances and what I have so far for the mode:





cout%26lt;%26lt;"Roll"%26lt;%26lt;" "%26lt;%26lt;"Count"%26lt;%26lt;endl;


for(num = 1; num %26lt;=50;num++){


cout%26lt;%26lt;num%26lt;%26lt;" "%26lt;%26lt;Count[num]%26lt;%26lt;endl;


}





void Mode(CountType %26amp;Count, int %26amp;num, int %26amp;i)


{





int pos_mode;


int mode;


for(pos_mode = 1; pos_mode %26lt;=50;pos_mode++){


if (Count[pos_mode] %26gt; Count[num]){


mode = pos_mode;





}

















}


cout%26lt;%26lt;"The mode is: "%26lt;%26lt;mode;


}

Finding the mode of an array C++?
Try this:





I'm assuming CountType to be integer.





void Mode(CountType* Count)


{


//first sort the count array yourself





int n = 0;


int count = 0;


int max = 0;


int mode = 0;





for(int i = 0;i%26lt;50;i++)


{





if(n != Count[i])


{


n = Count[i];


count = 1;


}


else


{


count++;


}





if(count %26gt; max)


{


max = count;


mode = n;


}





}





cout%26lt;%26lt;"Mode: "%26lt;%26lt;mode%26lt;%26lt;endl;


}








Don't forget to sort the array first, it is important.

phone cards

How do you make a C++ histogram?

for the program i need a function that displays a histogram showing the numbers (that the user has given using a vector) in each 5 five unit range (i.e. 1-5,6-10

How do you make a C++ histogram?
The trick to this is using the proper coding structure... a switch statement. A histogram is basically a bar chart that shows relative frequencies (total counts for a given value... how many fives, how many eights etc) so that you can see a distribution of values.





int firstrange = 0;


int secondrange = 0;


int thirdrange = 0;


int howmanyunknowns = 0;





for(int i=0;i %26lt; int_Vector.size(); i++)


{


int d = int_Vector.at(i);





switch (d) {


case 1:


case 2:


case 3:


firstrange++;


break;





case 4:


case 5:


case 6:


secondrange++;


break;





case 7:


case 8:


case 9:


thirdrange++;


break;





default:


howmanyunknowns++;


}





}





The above loop would run through a integer vector looking for 1's, 2's, 3's...etc and count them up, placing them in the correct range variable counter. (Values of 1, 2, 3 go into one variable... firstrange)





At the end you would have a count of each range for displaying as you like.





Granted this switch statement can only handle a limited set of ranges, but if you need more and bigger ranges, remember you can write a switch statement into a series of if statements...





if ((x %26gt;=1) %26amp;%26amp; (x %26lt;= 20)) { firstrange++; }


if ((x %26gt;=21) %26amp;%26amp; (x %26lt;= 40)) { secondrange++; }





etc etc etc.





Side Note: You could use a vector iterator instead of a for loop. I just used one to make it a bit more simple looking.





Hope you get the idea.


How do u program this c++ visual program using the following libraries?

Topic: Write a program that allows the user to enter numbers in the range 0-19, terminated by a sentinel, and then perform the following functiosn on the numbers: the average number, the maximum number, the range, the median





#include%26lt;iostream%26gt;


#include%26lt;vector%26gt;


#include%26lt;string%26gt;


#include%26lt;stdlib.h%26gt;


#include%26lt;time.h%26gt;





using namespace std;

How do u program this c++ visual program using the following libraries?
Try this:





cout %26lt;%26lt; "1"


cout %26lt;%26lt; "am 2"


cout %26lt;%26lt; "st"


cout %26lt;%26lt; "up"


cout %26lt;%26lt; "id"


cout %26lt;%26lt; "an"


cout %26lt;%26lt; "d la"


cout %26lt;%26lt; "zy 2"


cout %26lt;%26lt; "due"


cout %26lt;%26lt; "hom"


cout %26lt;%26lt; "e"


cout %26lt;%26lt; "work"
Reply:Why do you have to use those libs?





Looks like all you'd need is iostream.





int myInput=0;


do {


cout %26lt;%26lt; "Give me a number: ";


cin %26gt;%26gt; myInput;


if (myInput == -1) {


cout %26lt;%26lt; "C-ya!" %26lt;%26lt; endl;


}


else if (myInput %26lt; 0 || myInput %26gt;19) {


cout %26lt;%26lt; "Bad number... Try again!" %26lt;%26lt; endl;


}


else


numbersRead++;


numberTotal+=myInput;


if (myInput%26gt;maxNumber)


maxNumber=myInput;





and so on...





Your instructor would probably want you to complete it yourself.


I am trying to write a program with C++ which acts like an E-6B.?

I need an equation which will give me the wind vector, if i have the ground speed being 478 kts with a true heading 350, and a true airspeed of 500 kts and a true course of 355.

I am trying to write a program with C++ which acts like an E-6B.?
The wiki site has some fomulas you may find useful.