# having difficulty using a for loop to solve an equation

6 messages
Open this post in threaded view
|
Report Content as Inappropriate

## having difficulty using a for loop to solve an equation

 here is my task: Create two arrays x and y, whose entries are defined as x  = i, y  = j i = 1, 2, 3, 4, 5. y=5 − 2j, j = −2, −1, 0, 1, 2. Then, compute the cubed sum of these two vectors in two different ways. First, use a for loop to construct the vector z as z  = (x + y )^ 3 , i = 1, 2, 3, 4, 5. my code: %Vectorization clc; clear; %x=i;                %array created per spec %i=[1 2 3 4 5];      %array created per spec y=5-2j;             %array created per spec j=[-2 -1 0 1 2];    %array created per spec z=(y).^3;           %formula to test for loop for m=j   t=z end   the result: t =   65 - 142i t =   65 - 142i t =   65 - 142i t =   65 - 142i t =   65 - 142i I know the answer should be t= (the cube for the elements in j) I'm new to octave but I've always needed adult supervision with loops.  What am I missing?
Open this post in threaded view
|
Report Content as Inappropriate

## Re: having difficulty using a for loop to solve an equation

 "i" and "j" are sqrt(-1). Use something else "ii" and "jj" is what I normally do.Dmitri.On Mon, Jun 19, 2017 at 6:47 PM, naryana.shankara wrote:here is my task: Create two arrays x and y, whose entries are defined as x  = i, y  = j"i" and "j" are sqrt(-1). Use something else. My choise :  "ii" and "jj" Dmitri.--​ ​ _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|
Report Content as Inappropriate

## Re: having difficulty using a for loop to solve an equation

 In reply to this post by naryana.shankara got rid of i and j my code looks like this:%Vectorizationclc;clear;a=[1 2 3 4 5];      %array created per specx=a;                %array created per specb=[-2 -1 0 1 2];    %array created per specy=5-2.*b;           %array created per spec  z=(y+x).^3;         %formula to be solved in loopfor m=a  t=zend and the output is:t =   1000    729    512    343    216t =   1000    729    512    343    216t =   1000    729    512    343    216t =   1000    729    512    343    216t =   1000    729    512    343    216>>I was expecting my result to be something along the lines of:t =   1000    729    512    343    216>>ort=1000t=729t=512t=343t=216>>Did I leave something out or forget to add something?Thank you for your helpOn Mon, Jun 19, 2017 at 5:47 PM, naryana.shankara wrote:here is my task: Create two arrays x and y, whose entries are defined as x  = i, y  = j i = 1, 2, 3, 4, 5. y=5 − 2j, j = −2, −1, 0, 1, 2. Then, compute the cubed sum of these two vectors in two different ways. First, use a for loop to construct the vector z as z  = (x + y )^ 3 , i = 1, 2, 3, 4, 5. my code: %Vectorization clc; clear; %x=i;                %array created per spec %i=[1 2 3 4 5];      %array created per spec y=5-2j;             %array created per spec j=[-2 -1 0 1 2];    %array created per spec z=(y).^3;           %formula to test for loop for m=j   t=z end the result: t =   65 - 142i t =   65 - 142i t =   65 - 142i t =   65 - 142i t =   65 - 142i I know the answer should be t= (the cube for the elements in j) I'm new to octave but I've always needed adult supervision with loops.  What am I missing? -- View this message in context: http://octave.1599824.n4.nabble.com/having-difficulty-using-a-for-loop-to-solve-an-equation-tp4683785.html Sent from the Octave - General mailing list archive at Nabble.com. _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|
Report Content as Inappropriate

## Re: having difficulty using a for loop to solve an equation

 In reply to this post by naryana.shankara On Tue, Jun 20, 2017 at 1:47 AM, naryana.shankara wrote:here is my task: Create two arrays x and y, whose entries are defined as x  = i, y  = j i = 1, 2, 3, 4, 5. y=5 − 2j, j = −2, −1, 0, 1, 2. Then, compute the cubed sum of these two vectors in two different ways. First, use a for loop to construct the vector z as z  = (x + y )^ 3 , i = 1, 2, 3, 4, 5. my code: %Vectorization clc; clear; %x=i;                %array created per spec %i=[1 2 3 4 5];      %array created per spec y=5-2j;             %array created per spec j=[-2 -1 0 1 2];    %array created per specFirst of all, I would say it is an ill-formed question.In the introduction, two arrays are defined, however the position is also defined, especially for the y-array.So in a formal sense only z1 and z2 are defined.When declaring your array, first define the index then use it.j=-2:2;y=5-2*j;Likewise for x. _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|
Report Content as Inappropriate

## Re: having difficulty using a for loop to solve an equation

 In reply to this post by naryana.shankara On Tue, Jun 20, 2017 at 2:26 AM, shankara naryana wrote:got rid of i and j my code looks like this:%Vectorizationclc;clear;a=[1 2 3 4 5];      %array created per specx=a;                %array created per specb=[-2 -1 0 1 2];    %array created per specy=5-2.*b;           %array created per spec  z=(y+x).^3;         %formula to be solved in loopfor m=a  t=zend Actually you already wrote the vectorized form. There is no need for the for loop anymoreFor the for loop solution, you would need to calculate the solution for each index separately.I do not know if my other mail came through. There are network problems in the Netherlands.But I think the assignment is ill-formed.Purely from a methematical perspective, only z_1 and z_2 are defined. _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave