Formula for z into Matlab

1. Oct 18, 2006

Benny

If I have some data for example (x_1,y_2) = (1,1), (x_2,y_2) = (2,2) and (x_3,y_3) = (3,3) [they aren't coordinates, it's just a convenient way to show the information] and I want to evaluate the values of z (z_1, z_2 and z_3) where for example z = x^2 + y^6, can I enter the formula for z into Matlab to do this for me?

For just 3 bits of data I can do it by hand but if I have 15+ then it gets tiresome so I'd like to know if I can use Matlab to apply a formula to data (like the example above) to get out the required values. Any help would be good, thanks.

2. Oct 18, 2006

SGT

I suppose that by x and y you mean two column vectors. The operation ^ is not defined for vectors, only for scalars and square matrixes, but in Matlab you can use the elementwise operation .^:
z = x.^2 + y.^6

3. Oct 19, 2006

Benny

So for example could I do the following to obtain values of z?

x = [1 2 3 4];
y = [1 2 3 4];
z = x.^2+y.^4

That sort of method is ok for a few values. But if I have 50 or so pieces of data then it's impractical to enter column vectors with 50 elements into Matlab. Is it possible to transfer/import, the data entered into the worksheets of Excel, to Matlab and use commands in Matlab to apply some function (for example z = x^2+y^2) on the data (x,y) to obtain values of z?

4. Oct 19, 2006

SGT

Yes, this is a valid command.
For use of Matlab with Excel and other Windows applications, read the Matlab Help about Dynamic Data Exchange (DDE).
Alternatively, you can save the columns of your Excel worksheet as a text file and use the load Matlab function to import them as a matrix.
For instance, suppose you have a worksheet with 2 columns and you save it as test.txt. In Matlab you can use the command load test.txt in the appropriate directory. You have in your work directory a matrix named test.
Now you can use the command z = test(:,1).^2 + test(:,2).^4 to get your result.

5. Oct 20, 2006

Benny

Ok thanks. I ended up just trying testing some things by putting in some random numbers into a column of excel, pasting it into a 'half-complete' vector in Matlab, closed out the vector with the square bracket na dthen applied some element wise operations. It worked so that was good.