I need to solve an equation involving squares and cubes.

2 views (last 30 days)
I need to sole this equation for L, and I have a set of values for d and p.
p = -2(d/L)^3 + 3(d/L)^2
Im wondering how to put this into Matlab. Any help would be apprecitated.
  1 Comment
James Tursa
James Tursa on 12 Nov 2019
Multiply both sides by L^3 and then you have a polynomial in L to work with.

Sign in to comment.

Accepted Answer

Dimitris Kalogiros
Dimitris Kalogiros on 12 Nov 2019
Edited: Dimitris Kalogiros on 13 Nov 2019
It is a 3rd order equation, it has only an analytical solution.
You can use the following code:
clc; clearvars;
syms p d L
p=1;
d=3;
eq= p == -2*(d/L)^3 + 3*(d/L)^2
% analytical solution
solve(eq,L)
%numerical solution
vpasolve(eq)
And in case you want to run this for a set of values , you can do the following:
clc; clearvars;
syms p d L
% values for p, d
pValues=50:10:90;
dValues=300:50:600;
% an empty buffer for the solutions, combined with p,d
SolBuffer=cell(length(pValues)*length(dValues),3);
% solution counter
k=0;
for n=1:length(pValues)
for m=1:length(dValues)
k=k+1;
p=pValues(n);
SolBuffer{k,1}=p;
d=dValues(m);
SolBuffer{k,2}=d;
eq= p == -2*(d/L)^3 + 3*(d/L)^2;
% analytical solution
solve(eq, L);
%numerical solution
SolBuffer{k,3}=vpasolve(eq).';
end
end
% An example: the third solution
disp('data p and d :')
disp(SolBuffer{3,1})
disp(SolBuffer{3,2})
disp('solutions:')
disp(SolBuffer{3,3})
  5 Comments
Thomas McCormack
Thomas McCormack on 13 Nov 2019
Thats very kind of you, thanks a lot!! One final thing, is there a way to print all the answers in a matrix?
Dimitris Kalogiros
Dimitris Kalogiros on 13 Nov 2019
clc; clearvars;
syms p d L
% values for p, d
pValues=50:10:90;
dValues=300:50:600;
% an empty buffer for the solutions, combined with p,d
SolBuffer=nan(length(pValues)*length(dValues),3);
% solution counter
k=0;
for n=1:length(pValues)
for m=1:length(dValues)
k=k+1;
p=pValues(n);
d=dValues(m);
eq= p == -2*(d/L)^3 + 3*(d/L)^2;
% analytical solution
solve(eq, L);
%numerical solution
SolBuffer(k,1:3)=vpasolve(eq).';
end
end
% display solutions
SolBuffer

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 12 Nov 2019
You have many values for both d and p? Do you want to solve it for all combinations of d and p?
Anyway, first, I would recognize that d and L are alwways combined in the same form, thus, we have d/L always. So first, substitute
X = d/L
Of course, once we know the value of X, we can always recover L, as:
L = d/X
Now your problem reduces to
p = -2*X^3 + 3*X^2
You could use a loop over all values of p, getting three roots for each value of p. Before we do even that however, do a plot.
ALWAYS PLOT EVERYTHING.
fun = @(X) -2*X.^3 + 3*X.^2;
fplot(fun,[-1,2])
yline(0);
yline(1);
See that I used the .^ operator there to avoid problems.
untitled.jpg
Because this is a cubic polynomial in X, you can think of your problem as having 3 real roots for X, whenever p is betweeen 0 and 1. For p larger than 1 or smaller than 0, the problem will have one real root and two complex roots.
For example,
polycoef = @(p) [-2 3 0 -p];
roots(polycoef(0))
ans =
0
0
1.5
roots(polycoef(-.00001))
ans =
1.5 + 0i
-1.1111e-06 + 0.0018257i
-1.1111e-06 - 0.0018257i
So if p is just slightly less than zero as I predicted, we see a real root, but then always two complex conjugate complex roots. The same thing happens at p==1, where 3 real roots turn into a real and two complex roots as we go above p=1.
For p between 0 and 1 however, three real roots for X.
roots(polycoef(0.5))
ans =
1.366
0.5
-0.36603
Again, if you then know the value of d, we can easily recover the value of L, as
L = d./X

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!