help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: shankara naryana
Subject: Re: having difficulty using a for loop to solve an equation
Date: Mon, 19 Jun 2017 18:26:48 -0600

got rid of i and j my code looks like this:

%Vectorization
clc;
clear;
a=[1 2 3 4 5];      %array created per spec
x=a;                %array created per spec
b=[-2 -1 0 1 2];    %array created per spec
y=5-2.*b;           %array created per spec 
z=(y+x).^3;         %formula to be solved in loop
for m=a
  t=z
end

and the output is:

t =

   1000    729    512    343    216

t =

   1000    729    512    343    216

t =

   1000    729    512    343    216

t =

   1000    729    512    343    216

t =

   1000    729    512    343    216

>>

I was expecting my result to be something along the lines of:

t =

   1000    729    512    343    216
>>

or

t=
1000
t=
729
t=
512
t=
343
t=
216
>>

Did I leave something out or forget to add something?

Thank you for your help


On Mon, Jun 19, 2017 at 5:47 PM, naryana.shankara <address@hidden> wrote:
here is my task:
<http://octave.1599824.n4.nabble.com/file/n4683785/Screenshot_2017-06-19_17-54-18.png>

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
address@hidden
https://lists.gnu.org/mailman/listinfo/help-octave


reply via email to

[Prev in Thread] Current Thread [Next in Thread]