Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Why does my code not function properly?

1 Ansicht (letzte 30 Tage)
Dania Abdelqader
Dania Abdelqader am 18 Okt. 2015
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
Elimit= 0.001;
nlimit= 10000;
n=0;
prompt= 'input a value for a : ';
a=input (prompt);
prompt= 'input a value for b: ';
b=input(prompt);
f(1) = 16*(a)^2 + 3*(a)-1;
f(2) = 16*(b)^2 + 3*(b)-1;
if f(1)==0
fprintf('The root is %f = ', a);
else
if f(2)==0
fprintf('The root is %f = ',b);
end
end
while (f(1)*f(2)>0)
prompt = 'Please insert a new value for b: ';
b=input (prompt);
if f(1)==0
fprintf('The root is %f = ', a);
if f(2)==0
fprintf('The root is %f = ',b);
end
end
end
c = a+b/2;
Ere = abs(b-a/c);
while (Ere > Elimit && n< nlimit)
n = n+1;
f(3) = 16*(c)^2 + 3*(c)-1;
if f(3)==~0
fprintf ('The root is %f= ', c);
return
else if f(3) * f(2)>0
b=c;
else
a=c;
end
end
end
  2 Kommentare
Stephen23
Stephen23 am 18 Okt. 2015
Bearbeitet: Stephen23 am 18 Okt. 2015
What does "Why does my code not function properly" actually mean? If you actually write what the code does that you do not expect, then we do not have to rely on our mind-reading skills.
Does "not function properly" mean one of these?:
  • the code made my computer explode
  • the code deleted the internet
  • the code produced an incorrect result
  • the code crashes my computer
  • the code results in an error message (and here it is...)
  • the code does not run when I click on my GUI button that I have not told you anything about...
It also helps us (and you too!) if you write tidy, readable code, with consistent indentation. And then formatting this properly that we can follow your code too:
John D'Errico
John D'Errico am 18 Okt. 2015
Bearbeitet: John D'Errico am 18 Okt. 2015
Why do you think it is not functioning "properly" Define proper, and say what it does that is not proper please? Make it easy for someone to answer your question, rather than forcing them to run your code, then figure out what they think it should have done. As it is, a quick glance at the code shows any number of issues that COULD cause problems.

Antworten (1)

Joseph Jennings
Joseph Jennings am 19 Okt. 2015
Dania,
After carefully reviewing your code, one thing I would like to point out is that declaring a variable F(1) or F(2) or F(3) should be done differently in your code. Now I could be mistaken so let me apologize upfront if I am wrong, to the best of my knowledge you should create a single variable F=[equation 1, equation 2, equation 3] and then call them by saying F(1) or F(2) or F(3), in matlab when you use a variable name with parenthesis the way that you have done it, matlab is looking for the 1st, 2nd or 3rd entry in the variable F. If you do not wish to create an array and call each entry in the array back you could simply call each one F1 F2 and F3. However, when you write "if f(2)==0" matlab is interpreting that as "if the second entry in the variable f is equal to exactly 0" if that is what you meant then you need to create an array with 3 entries in order to get your code to function the way it is written.
I hope this helps and again I apologize in advance is this wasn't what you were looking for however, the question as stated by the previous responses needs to be better written so others can understand what you're really asking. best of luck

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by