Looping question

2 views (last 30 days)
Mahesh Ramaraj
Mahesh Ramaraj on 9 Mar 2011
Hi, I have a question regarding using loop for a function file. For example, let func represent a function file, where f = x^2 - 1 and f is the output argument and x is the input argument.
Now, my x = 1:100. and now for i = 1:100 z(i) = func(x(i)); end
f_n = min(f).
My question: Is there a way where I can identify the particular x for which f_n is giving out the value for?
Thanks, Mahesh Ramaraj.

Accepted Answer

Sean de Wolski
Sean de Wolski on 9 Mar 2011
No reason for loops!
x = 1:100; %x vector
func = @(x)x.^2-1; %function to apply
[the_min, idx] = min(func(x)); %find the minimum and index of minimum
min_x = x(idx); %extract x corresponding to the minimum of f(x)
  1 Comment
Mahesh Ramaraj
Mahesh Ramaraj on 10 Mar 2011
Thanks a lot...it worked like a charm....

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!