필터 지우기
필터 지우기

How do I run increments of numbers through a function?

조회 수: 8 (최근 30일)
Phi Tran
Phi Tran 2018년 2월 9일
답변: Walter Roberson 2018년 2월 9일
I would like to know how to run numerous numbers through a function. For example, I would like to start at number 95 and decrease by increments of .1.
so 95.0000, 94.9000, 94.8000, 94.7000, 94.6000, 94.5000, 94.4000....... and so on...
I want to run these numbers to the attached function "m.m" file until the output generated is 35

답변 (2개)

Eric Tao
Eric Tao 2018년 2월 9일
Use MATLAB colon expression, and you don't need for-loop anymore.
Run:
a = [95:-0.1:35]';
you will get the result.
  댓글 수: 1
Stephen23
Stephen23 2018년 2월 9일
As you are not concatenating anything use parentheses, not square brackets:
a = (95:-0.1:35)';

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2018년 2월 9일
L = 95;
while true
output = m(L);
fprintf('For L = %f, output = %g\n', L, output);
if output == 35
break;
end
L = L - 0.1;
end
However.... there is no guarantee that m() will ever give an output of exactly 35. http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
If the task were to find the L such that m(L) == 35, then you would not proceed sequentially: you would use fzero or fsolve:
fzero(@(L) m(L)-35, 95)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by