Matlab beginner, making variables

AI Thread Summary
In MATLAB, to create a variable that depends on another variable, you can directly use vector operations. For example, if 'a' is defined as a vector, you can compute 'y' using the formula y = 2 * a + 3, which will yield a vector of results corresponding to each element in 'a'. To create more complex dependencies, consider using anonymous functions or function handles, which allow for dynamic calculations without needing separate m-files. Resources like the Mathworks site provide helpful guidance on these features. This approach ensures that when 'a' changes, 'y' updates automatically based on the defined relationship.
Dell
Messages
555
Reaction score
0
in MATLAB how do i make a variable which is dependant on another variable, for example

y=2*a+3 for any a,
so that when i change a, y will automatically change

i have only managed to set a value for a, then set y and i get a fixed value for y
 
Physics news on Phys.org
Dell said:
in MATLAB how do i make a variable which is dependant on another variable, for example

y=2*a+3 for any a,
so that when i change a, y will automatically change

i have only managed to set a value for a, then set y and i get a fixed value for y

Right, and you can place whatever value or values in 'a' that you like. For instance, if 'a' is a vector containing 7 different values, and the formula is written correctly, then 'y' will contain 7 values, each that function of the corresponding element in 'a':

>> a = [-1 0 1 2 3 4 5]

a =

-1 0 1 2 3 4 5

>> y = 2 * a + 3

y =

1 3 5 7 9 11 13
-Will Dwinnell
http://matlabdatamining.blogspot.com/"
 
Last edited by a moderator:
Sounds like you're looking for functions. If you're function is simple, you can use an anonymous function (no separate m-file required) or you can use a separate m-file function with a function handle.

Check out the Mathworks site and search for 'Anonymous Functions' or 'Function Handles' (or search within MATLAB's help).

Is this what you're looking for?

-Kerry
 

Similar threads

Replies
11
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
2
Views
2K
Back
Top