Insert Equation directly into the code

조회 수: 4 (최근 30일)
WhatIsMatlab-
WhatIsMatlab- 2016년 2월 12일
답변: Tavaris Jones 2019년 3월 10일
This code is for finding roots for an equation. My code runs fine how it is. But I do not like how I have to use the input command to call the function. I want the code to run with the function in the code.
The function I am using is x^2-6. But I cannot not seem to make it happen. It says x is not a defined variable. How do I fix this?
f1 = input('Equation','s'); %%%%I want f1 = x^2-6
f = inline(f1);
xl = 0;
xu = 10;
xe = (xl+xu)/(2);
acc = 0.0001;
while abs(f(xe))> acc
i = i + 1;
if (f(xe)*f(xu)) < 0
xl = xe;
else
xu = xe;
end
xe = (xl+xu)/2;
end
fprintf('%d)\t%f\tf(x)=%f\n',i,xe,f(xe));

채택된 답변

Benjamin Kraus
Benjamin Kraus 2016년 2월 12일
편집: Benjamin Kraus 2016년 2월 12일
You want to use an anonymous function :
f = @(x) x^2-6;
  댓글 수: 1
WhatIsMatlab-
WhatIsMatlab- 2016년 2월 12일
Thank you!! That's exactly what I wanted.

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

추가 답변 (1개)

Tavaris Jones
Tavaris Jones 2019년 3월 10일
x-14=-2y-3z

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by