Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

how to create a function m-file in Matlab v.7.12?

Asked by MissA on 12 Dec 2012

How to create a function m-file to evaluate the function f(x)=((1+a)*sin(a*x)*exp(-a*x))/x

where a is given and a=0.3

??

Please Help

Thanks in advance

MissA

0 Comments

MissA

Products

No products are associated with this question.

2 Answers

Answer by Walter Roberson on 12 Dec 2012
function f = my_func(x)
  a = 0.3;
  f = ((1+a) * sin(a*x) .* exp(-a*x)) ./ x;
end

0 Comments

Walter Roberson
Answer by Muruganandham Subramanian on 12 Dec 2012
[f]=my_func(x)
 a=0.3;
 f(x)=((1+a)*sin(a*x)*exp(-a*x))/x
 return;

3 Comments

MissA on 12 Dec 2012

error pops up saying ??? Undefined function or variable 'x'. what do I do with an X then?

Thanks

Muruganandham Subramanian on 13 Dec 2012

If you want to write as a function file, this is the way, and 'x' is an i/p argument, that you can read it from workspace, beore running the function file, like >>x=3; % 'x' is variable

    function f =my_func(x)
       a=0.3;
       f(x)=((1+a)*sin(a*x)*exp(-a*x))/x
       return;

otherwise, you can do this using for looping operation.

Walter Roberson on 13 Dec 2012

If you try that and x contains any values that are not positive integers, then MATLAB will complain about you trying to index the array f. For example, if x was 1/2 then

f(x) = .....

would mean

f(1/2) = .....

and that is trying to assign a value to element # (1/2) in array f, which is not legal.

See my solution.

Muruganandham Subramanian

Contact us