so I am trying to make a cubic equation calculater in matlab but i keep getting the error :
Error using matlab.ui.control.internal.model.AbstractNumericComponent/set.Value (line 111)
'Value' must be a double scalar.
I am really not sure what it means but here is the code:
vala = app.a.Value;
valb = app.b.Value;
valc = app.c.Value;
vald = app.d.Value;
zz1 =(-valb/3*vala)-(1/3*vala)*(0.5*(2*valb^3-9*vala*valb*valc+27*vala^2*vald+sqrt(2*valb^3-9*vala*valb*valc+27*vala^2*vald)^2-(4*valb^2-3*vala*valc)^3)^0.5)^1/3-(1/3*vala)*(0.5*(2*valb^3-9*vala*valb*valc+27*vala^2*vald-sqrt(2*valb^3-9*vala*valb*valc-27*vala^2*vald)^2-(4*valb^2-3*vala*valc)^3)^0.5)^1/3;
zz2 =(-valb/3*vala)+(1+1i*sqrt(3)/6*vala*(0.5*(2*valb^3-9*vala*valb*valc+27*vala^2*vald+sqrt(2*valb^3-9*vala*valb*valc+27*vala^2*vald)^2-(4*valb^2-3*vala*valc)^3)^0.5)^1/3)+(1-1i*sqrt(3)/6*vala)*(0.5*(2*valb^3-9*vala*valb*valc+27*vala^2*vald-sqrt(2*valb^3-9*vala*valb*valc+27*vala^2*vald)^2-(4*valb^2-3*vala*valc)^3)^0.5)^1/3;
zz3 =(-valb/3*vala)+(1-1i*sqrt(3)/6*vala*(0.5*(2*valb^3-9*vala*valb*valc+27*vala^2*vald+sqrt(2*valb^3-9*vala*valb*valc+27*vala^2*vald)^2-(4*valb^2-3*vala*valc)^3)^0.5)^1/3)+(1+1i*sqrt(3)/6*vala)*(0.5*(2*valb^3-9*vala*valb*valc+27*vala^2*vald-sqrt(2*valb^3-9*vala*valb*valc+27*vala^2*vald)^2-(4*valb^2-3*vala*valc)^3)^0.5)^1/3;
app.x1.Value =zz1;
app.x2.Value =zz2;
app.x3.Value =zz3;
p.s if you know any simpler ways that would be big help

7 comentarios

Rik
Rik el 7 de Mzo. de 2021
Doesn't the error message explain what is the problem? You need to use a scalar double. That means you can only set a single value, not multiple.
What are the values of zz1, zz2, and zz3?
Mohamed Rashed
Mohamed Rashed el 7 de Mzo. de 2021
Those are supposed to be the outputs i am currently away from my computer but i have changed the code to make it a bit easier to trouble shoot due to too many errors
Walter Roberson
Walter Roberson el 7 de Mzo. de 2021
One of zz1, zz2, zz3 is empty, or non-scalar, or not a double .
Is that roots of a cubic ? I see you are using imaginary components. At the moment I do not know if you are permitted to set the object to complex values.
Mohamed Rashed
Mohamed Rashed el 8 de Mzo. de 2021
Yeah u have been trying to make this equation work for a while now but no luck i think that the equation itself and the coding dont work so gonna try some more thing to find the roots of a cubic twomorrow open to any suggestions.
Walter Roberson
Walter Roberson el 8 de Mzo. de 2021
Use roots() to find roots of the cubic.
And experiment with assigning real(zz1) and so on to the fields to test out whether really it is complaining about complex values.
Mohamed Rashed
Mohamed Rashed el 8 de Mzo. de 2021
do you maybe now where i could find how to display an error message on the app aswell like if someone puts in the polynomial x^2+x+1 or other impossible equations?
Walter Roberson
Walter Roberson el 8 de Mzo. de 2021
"puts in the polynomial x^2+x+1" -- puts in how? Are you expecting that app.b.Value will be a formula instead of a numeric value? If you are expecting a numeric value, then App Designer permits constructing a numeric edit field that does not permit arbitrary text.

Iniciar sesión para comentar.

 Respuesta aceptada

Aghamarsh Varanasi
Aghamarsh Varanasi el 12 de Mzo. de 2021
Editada: Aghamarsh Varanasi el 12 de Mzo. de 2021
Hi Rashed,
As suggested by Walter, you can use the root function in MATLAB to find the roots of a cubic polynomial.
From the error that you have mentioned, it seems that you are using a Edit Field (numeric) for x1, x2 and x3. As you are solving a cubic equation, there might be a chance that the root could be a complex number. As a workaround I would suggest you to use Edit Field (Text) of MATLAB App Designer for fields x1, x2 ,x3 and use num2str to convert the roots to string.
So the code would be:
vala = app.a.Value;
valb = app.b.Value;
valc = app.c.Value;
vald = app.d.Value;
p = [vala valb valc vald];
r = root(p);
% here x1, x2 and x3 are Edit Field (Text)
app.x1.Value = num2str(r(1));
app.x2.Value = num2str(r(2));
app.x3.Value = num2str(r(3));
Hope this helps

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by