Using An Equation As An Input Argument For A Function

조회 수: 1 (최근 30일)
Mike
Mike 2014년 3월 31일
댓글: Mike 2014년 4월 1일
Hello,
I am trying to write a function that takes an equation (fx) in as input argument and solves for it using two separate bounds (fl) (fu).
For example say the equation is: x^8-1.5=0
How would I go about getting the equation input as my fx value? Do I have to use f=@x somehow?
Thanks in advance for any help. ~Mike

답변 (1개)

Mischa Kim
Mischa Kim 2014년 3월 31일
Mike, check out the code below:
function fun_test()
f = @(x) x^8 - 1.5;
xl = 0;
xu = 1;
sol = fun_solve(f, xl, xu);
disp(sol)
end
function fsol = fun_solve(f,xl,xu)
x = (xu + xl)/2;
fsol = f(x);
end
Since I'm not quite sure what exactly you would like to solve for, I am simply evaluating the function at the midpoint.
  댓글 수: 1
Mike
Mike 2014년 4월 1일
Mischa,
My actual problem ended up just being that I was not using parenthesis for my variable.
ie @x instead of @(x)
Thanks for the response though.

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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by