MATLAB: Solve a cubic equation using MATLAB code

cubic eqn

I have a cubic equation whose coefficients are varying according to a parameter say w in the following manner:
a=2/w;
b=(3/w+3);
c=(4/(w-9))^3;
d=(5/(w+6))^2;
a*(x^3)+b*(x^2)+c*x+d=0
I want to solve the above equation using a m-file not in the command window. Is it possible ? I am fairly new to MATLAB, so help would be appreciated.

Best Answer

From your comments, it looks like you want this instead:
function [] = solve_cubic()
w=10:2:30;
a=4*((9*w)-7);
b=(-2)*((18*w)-16).*(w+1);
c=3*((w+1).^2).*((3*w)-1);
d=(-3)*((1+w).^3);
f = zeros(1,length(w));
for ii = 1:numel(w)
g = roots([a(ii),b(ii),c(ii),d(ii)]);
f(ii) = g(g<1);
end
ptr=2./(1+w);
u=(1+w)./2;
l=(u.*(1-ptr.*f).^3)./3;
plot(w,l);