Effacer les filtres
Effacer les filtres

Why I cant plot drawn in MATLAB app designer

1 vue (au cours des 30 derniers jours)
Anne
Anne le 7 Déc 2023
Commenté : Walter Roberson le 8 Déc 2023
methods (Access = private)
function [c, n] = BisectionMethod(app,fx, a, b, error)
fx = matlabFunction(str2sym(fx));
n = 0;
c=0;
while(1)
n = n + 1;
c = (a+b)/2;
if (fx(c)*fx(a) < 0)
b = c;
elseif (fx(c)*fx(a) > 0)
a = c;
end
e = abs(b-a);
if (e<error)
break;
end
end
end
end
function RunButtonPushed(app, event)
FUNC=app.FUNCEditField.Value;
a = app.aEditField.Value;
b = app.bEditField.Value;
error = app.errorEditField.Value;
[c,n] = BisectionMethod(app,FUNC,a,b,error);
app.resultEditField.Value = c;
app.number_of_iterationsEditField.Value = n;
x = a:(a+b)/0.01:b;
y = matlabFunction(str2sym(FUNC));
f = y(x);
plot(app.UIAxes,x,f);
  1 commentaire
Angelo Yeo
Angelo Yeo le 8 Déc 2023
In order to find the precise cause and solution for the issue you are currently experiencing, I believe that the following additional information would be necessary. If you could provide the following information, it would enable contributors, including myself, to assist you more effectively:
1) Example files and data files that can reproduce the issue
2) Exact steps to reproduce the same problem using the files you provided
3) All error messages that appear when the issue occurs

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 8 Déc 2023
while(1)
n = n + 1;
c = (a+b)/2;
if (fx(c)*fx(a) < 0)
b = c;
elseif (fx(c)*fx(a) > 0)
a = c;
end
e = abs(b-a);
if (e<error)
break;
end
end
Let us look at that more closely.
First, let us imagine that fx happens to be a function and the bounds a and b happen to be such that a and b are notably different, but that fx(a) is exactly 0. For example f might be (x-2)^2 and a might happen to be 2 and b might happen to be 6. Then c = (2+6)/2 = 3, and fx( c) --> fx(3) --> (3-2)^2 --> 1. Meanwhile fx(a) --> fx(2) --> (2-2)^2 --> 0 . The fx( c)*fx(a) would be 1*0 --> 0. 0 < 0 is false so the if false. The elseif re-calculates fx( c) --> 1 and fx(a) --> 0, fx( c)*fx(a) --> 1*0 --> 0. 0 > 0 is false so the elseif false. So both the if and elseif fail, so neither b nor a are assigned to in that logic. Now we caculate abs(b-a) --> abs(6-2) --> abs(4) --> 4. Is 4 < error? Probably not. So the break is not going to happen.
Next iteration... n gets incremented but a and b are still the same as they were, so you are going to have the same results as before, neither a nor b will change, the if and elseif still fail, the e value is still the same...
and around and around we go. The loop is not going to stop in this case.
Second, let us imagine that fx happens to be a function and the bounds a and b happen to be such that fx((a+b)/2) is exactly 0. For example f might be (x-2)^2 and a might happen to be -2 and be might happen to be 6 . Then c=(a+b)/2 --> (-2+6)/2 --> 4/2 --> 2. Then fx( c) --> (2-2)^2 --> 2 and fx(a) --> fx(-2) --> (-2-2)^2 --> 4^2 --> 16. Then 0 * 16 --> 0 . 0 < 0 is false so the if fails. The elseif calculates the same thing, 0*16 --> 0 and 0 > 0 is false, so the elseif fails. We can see that in this situation too, a and b are left unchanged and you have an infinite loop.
  1 commentaire
Walter Roberson
Walter Roberson le 8 Déc 2023
x = a:(a+b)/0.01:b;
y = matlabFunction(str2sym(FUNC));
Let us look a two cases: a = 2, b = 6, and a = -20, b = 6.
First case
x = 2:(2+6)/0.01:6
will be x = 2:8/0.01:6 which will be 2:800:6 which will be the same as x = 2. That is only one point, and when you plot() only one point then by default you get nothing at all showing up on the axes (the default is no marker, and lines are only created if there are at least two adjacent finite values.)
Second case
x = -20:(-20+6)/0.01:6
will be x = -20:-1400:6 and that is going to be empty. No plot.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Tags

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by