Insert Equation directly into the code

4 vues (au cours des 30 derniers jours)
WhatIsMatlab-
WhatIsMatlab- le 12 Fév 2016
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));

Réponse acceptée

Benjamin Kraus
Benjamin Kraus le 12 Fév 2016
Modifié(e) : Benjamin Kraus le 12 Fév 2016
You want to use an anonymous function :
f = @(x) x^2-6;
  1 commentaire
WhatIsMatlab-
WhatIsMatlab- le 12 Fév 2016
Thank you!! That's exactly what I wanted.

Connectez-vous pour commenter.

Plus de réponses (1)

Tavaris Jones
Tavaris Jones le 10 Mar 2019
x-14=-2y-3z

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by