How to plot the symbolic expression given by "root"

6 visualizaciones (últimos 30 días)
MGMKLML
MGMKLML el 24 de Mayo de 2017
Editada: Karan Gill el 17 de Oct. de 2017
Hello.
I want to solve the 3rd degree symbolic equation. I chose "solve" for this purpose. It gives the answer with "root(...)"; For example,
root(z^3 - (9*z^2)/10 + (17*z)/40 - 9/40, z, 1)
root(z^3 - (9*z^2)/10 + (17*z)/40 - 9/40, z, 2)
root(z^3 - (9*z^2)/10 + (17*z)/40 - 9/40, z, 3)
Now I want to plot this function, I mean, z^3 - (9*z^2)/10 + (17*z)/40. The problem is that this expression depends on the coefficients I give to the function. I could copy and paste this equation myself but then I need to do it everytime I change the coefficients which is pretty uncomfortable.
Is it possible to get the symbolic expression or just to make MATLAB plot the function somehow without doing it manually?
The code is here:
function draft14
clc;
fsys = @(x,y,z,c,mu1,s1,r2,b,s2) [c*y - mu1*x + x*z + s1; ...
r2*y*(1 - b*y) - x*y; ...
z - x*y + s2];
c = 0.2;
mu1 = 0.5;
s1 = 0.1;
r2 = 0.9;
b = 0.5;
s2 = 0;
par = [c mu1 s1 r2 b s2];
f = @(t,x) fsys(x(1),x(2),x(3),par(1),par(2),par(3),par(4),par(5),par(6));
tspan = 0:0.01:2;
y0 = [0.1 0.9 0.1];
[~,Y] = ode45(f,tspan,y0);
plot3(Y(:,1),Y(:,2),Y(:,3)); hold on;
plot3(y0(1),y0(2),y0(3), 'o','LineWidth',3);
xlabel('X'); ylabel('Y'); zlabel('Z');
figure;
plot(Y(:,1),Y(:,2));
xlabel('X'); ylabel('Y');
cubeq(par);
end
function cubeq(par) %Решает численно кубическое уравнение
c = par(1);
mu1 = par(2);
s1 = par(3);
r2 = par(4);
b = par(5);
s2 = par(6);
syms x
f = x^3 - r2*x^2 + (c + mu1*r2*b)*x - r2*(b*s1 + c);
s = solve(f,x);
disp('Решение кубического уравнения:');
disp(s);
clear x;
end
P.S. In the code above you can see than I set the parameters in the first function and then give it to the "cubeq" function. In the "cubeq" function I need to plot the cubic parabola, on which the roots depend.

Respuestas (3)

Karan Gill
Karan Gill el 25 de Mayo de 2017
Editada: Karan Gill el 17 de Oct. de 2017
If I understand correctly, you're asking " How can I automatically substitute values into a symbolic expression?". Use the subs function: https://www.mathworks.com/help/symbolic/subs.html
Googling " substitute value symbolic matlab " should show " subs " as the first result.
Here, use something like
params = [c mu1 ...];
paramValues = [0.2 0.5 ...];
f = subs(f,params,paramValues)
  2 comentarios
MGMKLML
MGMKLML el 25 de Mayo de 2017
Not exactly what I meant. I meant to plot the function which is given in the root expression, I mean, what stands under "root(...)"
Karan Gill
Karan Gill el 26 de Mayo de 2017
Then use children and fplot as Walter said.

Iniciar sesión para comentar.


Star Strider
Star Strider el 25 de Mayo de 2017
See if using vpasolve instead of solve does what you want:
s = vpasolve(f,x);

Walter Roberson
Walter Roberson el 25 de Mayo de 2017
Once you have a root() expression, you can use children() on it to extract the formula, after which you can plot as per usual, either by subs in numeric values or by fplot()

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by