MATLAB: I need to solve an equation involving squares and cubes.

MATLABsolving equations

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.

Best Answer

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})