Matlab Equation

Thread Starter

Cerkit

Joined Jan 4, 2009
287
Hi. How is it possible to write an equation for example f=X^2 so that anywhere in my code I can get the value of f for a given X by typing

F(X)

for some X value?

Thanks
 

steveb

Joined Jul 3, 2008
2,436
Hi. How is it possible to write an equation for example f=X^2 so that anywhere in my code I can get the value of f for a given X by typing

F(X)

for some X value?

Thanks
Simply create a function and put it in an m-file with the same name as your function. For example, for your squaring function chose a name like "Xsquared" for your function and store the function in a file called Xsquared.m. The function code in this m-file would look as follows in this case.

Rich (BB code):
function [output_value] = Xsquared(x)
output_value = x^2;
end
To call the function, simply do the following Y=Xsquared(2) and Y will be set equal to 4 (or the square of whatever value you plug in). Make sure the function file in in your working directory, or keep it in a directory that Matlab can access for functions.
 
Last edited:
Top