PDA

View Full Version : easy matlab ?


examinator
09-26-2008, 05:12 PM
Heres the the deal I have a program I'm writing in matlab for a class and thus far Ive made a function that saves values in an array.
array 1: x = [a1 a2 a3]
array 2: y = [b1 b2 b3]
the size of the array will be known, I need to use another function(already made) call it f to do the following: f(a1,b1), f(a2,b2) etc. I know how to do the iterating and loops and stuff but I don't know how to access the particular elements of the arrays. How do you access specific elements of arrays?

Bastian Gross
09-26-2008, 06:40 PM
function [y] = f(x1,x2)
y = 2*x1.^2 + 3*x2;
%End Function!!!!!!!!!!! This must be a seperate m-file : "f.m"

%array example
%array 1: x = [a1 a2 a3]
%array 2: y = [b1 b2 b3]
%
a1 = 1;
a2 = 2;
a3 = 1.5;
b1 = -1;
b2 = 0.5;
b3 = pi;

x = [a1 a2 a3];
y = [b1 b2 b3];

%and now insert this in the Command Window
y = f(x,y) % estimate y = -1.0000 9.5000 13.9248
%resp.
y = f(x(1),y(1))
y = f(x(2),y(2))
y = f(x(3),y(3))


This should work!!!!

atito
09-27-2008, 02:59 AM
Nop!
you have to change the last three lines:
z = f(x(1),y(1))
z = f(x(2),y(2))
z = f(x(3),y(3))

to have the 'expected' behavior

Bastian Gross
09-27-2008, 07:18 AM
Oh, my bad, my bad!
:smt024