Plot multiple graph after solving

1 visualización (últimos 30 días)
Sabrina Garland
Sabrina Garland el 23 de Ag. de 2023
Comentada: Sabrina Garland el 2 de Sept. de 2023

I have following two equations -

    f1 = -2*g*t.^3/((1 - y).^(1-2*g)) + 2*g*(3 - t.^2)^2./((2 - t).*y.^(1-2*g)) + (8*g*t)./(y.^(1-2*g)) - 2*g*(3 - 2*t).^2./((2 - t).*(1 - y).^(1-2*g)); 
    f2 = t - (((y.^(2*g))*(2/3) - (((1-y).^(2*g)).*((3 - 2*t)./(3*(2-t)))))./((y.^(2*g)).*(((3-t.^2))./(3*(2-t))) - ((1-y).^(2*g)).*(t/3)));

For every g in the range (0,0.114), solving the two equations above give two solution - y with t=1 and y with a t between 0.5 and 1. (it also give solution with negative y and t less than 0.5, I am not interested in them). To give a sense, for g=0.11 I get following solution - t=1 ^ y=0.85536 t=0.9873 ^ y=0.85530.

I want Matlab to plot two graph (on same plot) with g on x axis and y values in y axis. One graph should have y values for t=1 and one graph should have y values for t in range 0.5 to 1 (below 1).

Since I have done numerical simulation with random values, I know graph should look the following (attached rough sketch)

I don't know how to ask Matlab to pick the specific ans and then plot them. Shall I create a loop?

  5 comentarios
Sabrina Garland
Sabrina Garland el 23 de Ag. de 2023
Yes, both are equal to zero, sorry for the confusion
Sabrina Garland
Sabrina Garland el 24 de Ag. de 2023
@Torsten Please guide me how to proceed. I have just given the two equation name for Matlab to work with. Both f1 and f2 are equal to zero. I can solve them for one particular value of g but don't know how to store the obtained values and to create two graphs into same plot using that data

Iniciar sesión para comentar.

Respuesta aceptada

Torsten
Torsten el 24 de Ag. de 2023
Editada: Torsten el 25 de Ag. de 2023
syms y t g
f1 = -2*g*t.^3/((1 - y).^(1-2*g)) + 2*g*(3 - t.^2)^2./((2 - t).*y.^(1-2*g)) + (8*g*t)./(y.^(1-2*g)) - 2*g*(3 - 2*t).^2./((2 - t).*(1 - y).^(1-2*g));
[N1,D1] = numden(f1);
f2 = t - (((y.^(2*g))*(2/3) - (((1-y).^(2*g)).*((3 - 2*t)./(3*(2-t)))))./((y.^(2*g)).*(((3-t.^2))./(3*(2-t))) - ((1-y).^(2*g)).*(t/3)));
[N2,D2] = numden(f2);
tsol = solve(N2==0,t,'MaxDegree',3);
N1_subs_tsol1 = subs(N1,t,tsol(1));
N1_subs_tsol2 = subs(N1,t,tsol(2));
N1_subs_tsol3 = subs(N1,t,tsol(3));
G = 0.1:0.001:0.110;
soly1 = arrayfun(@(G)vpasolve(subs(N1_subs_tsol1/(y*(1-y)^(2*g)),g,G)==0),G);
solt1 = arrayfun(@(soly1,G)subs(tsol(1),[y,g],[soly1,G]),soly1,G);
soly2 = arrayfun(@(G)vpasolve(subs(N1_subs_tsol2/y/(-2*g),g,G)==0),G);
solt2 = arrayfun(@(soly2,G)subs(tsol(2),[y,g],[soly2,G]),soly2,G);
soly3 = arrayfun(@(G)vpasolve(subs(N1_subs_tsol3/y/(-2*g),g,G)==0),G);
solt3 = arrayfun(@(soly3,G)subs(tsol(3),[y,g],[soly3,G]),soly3,G);
hold on
p1 = plot(G,soly1,'r');
%plot(G,solt1,'r')
p2 = plot(G,soly2,'b');
%plot(G,solt2,'b')
p3 = plot(G,1./(1+(1/3).^(1./(1-2*G))),'g');
legend([p1,p2,p3],["slice 1","slice 2","slice 3"])
xlabel("g")
hold off
grid on
% Check the solutions
err11 = arrayfun(@(solt1,soly1,G)double(subs(f1,[t y g],[solt1 soly1 G])),solt1,soly1,G)
err11 = 1×11
1.0e-32 * 0 -0.0000 -0.0000 0.0000 0.0000 0.0000 0 -0.0000 0.0000 0.0000 0.1255
err12 = arrayfun(@(solt1,soly1,G)double(subs(f2,[t y g],[solt1 soly1 G])),solt1,soly1,G)
err12 = 1×11
1.0e-39 * 0 0 0 0 0.1837 0 0 0 0 0.1837 0
err21 = arrayfun(@(solt2,soly2,G)double(subs(f1,[t y g],[solt2 soly2 G])),solt2,soly2,G)
err21 = 1×11
1.0e-33 * 0.0000 0.0007 -0.0000 0.0000 0.9884 -0.0000 0.0001 0.0000 -0.3493 0.0000 -0.0000
err22 = arrayfun(@(solt2,soly2,G)double(subs(f2,[t y g],[solt2 soly2 G])),solt2,soly2,G)
err22 = 1×11
1.0e-39 * 0.3673 -0.5510 0.5510 -0.1837 -0.1837 0 -0.5510 0.1837 0 -0.5510 -0.9184
err31 = arrayfun(@(solt3,soly3,G)double(subs(f1,[t y g],[solt3 soly3 G])),solt3,soly3,G)
err31 =
1.0e-34 * Columns 1 through 10 0.0000 + 0.0000i 0.6181 + 0.4447i 0.0000 + 0.0000i 0.2703 + 0.4412i 0.0000 - 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 + 0.0000i Column 11 -0.0000 + 0.0000i
err32 = arrayfun(@(solt3,soly3,G)double(subs(f2,[t y g],[solt3 soly3 G])),solt3,soly3,G)
err32 =
1.0e-37 * Columns 1 through 10 0.0441 + 0.0073i 0.0478 - 0.0367i 0.0698 - 0.0073i -0.0808 - 0.0661i 0.0367 + 0.0147i -0.0624 - 0.0294i 0.0220 + 0.0220i -0.1102 - 0.0220i 0.0037 + 0.0073i 0.0220 - 0.0073i Column 11 0.0184 - 0.0514i
  22 comentarios
Torsten
Torsten el 2 de Sept. de 2023
You can edit the code according to your needs using the edit pencil and run it here with MATLAB online using the green RUN arrow.
Sabrina Garland
Sabrina Garland el 2 de Sept. de 2023
@Torsten Thank you a ton

Iniciar sesión para comentar.

Más respuestas (1)

Vikas
Vikas el 23 de Ag. de 2023
I read you question on the above that I understand you want to plot one to many graph in one graph.
For this you use "hold on" command
This is use to hold the previous graph after plot and then plot another graph that affect in same graph.
example:-
plot(3,6)
hold on
plot(44,23)
This two plot draw a graph in same only one graph.
As I wish you understand this answer?.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by