How to convert this function of one variable into MATLAB code?

3 visualizzazioni (ultimi 30 giorni)
Hi I'm trying to convert this function into MATLAB code.
I'm fairly new to understanding the syntax so maybe there's an obvious error to someone experienced.
I think I'm close with this but I'm expecting the function to pass through the x-axis.
This is my current code.
Many thanks!
a1 = 0.5;
n = 4;
S = 0.5;
K = S^n;
x1 = linspace(0,3);
x2 = ((K^2.*(1-(x1))+K*(1+(a1)).*(x1).^4-K.*(x1).^5)./(K.*(x1)+(x1).^5-(a1).*(x1).^4)).^(1/4);
plot(x1,x2, 'g')

Risposte (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 8 Nov 2021
You have done a good job, but there are a couple of small (imperative) points to consider - to take out real and imaginary parts of x2.
clc; clearvars
a1 = 0.5;
n = 4;
S = 0.5;
K = S^n;
x1 = linspace(0,3);
x2 = ((K^2*(1-(x1))+K*(1+(a1)).*x1.^4-K*x1.^5)./(K*x1+x1.^5-a1.*x1.^4)).^(1/4);
plot(x1,real(x2), 'r-x', x1, imag(x2), 'b'), grid on; legend('RE(x_2)', 'IM(x_2)', 'location', 'best')
xlabel('x_1'); ylabel('x_2')
  1 Commento
Angus K
Angus K il 8 Nov 2021
Modificato: Stephen23 il 8 Nov 2021
Hi,
Many thanks for this. Did my SYNTAX look okay? When I plotted the function in DESMOS the function looks slightly different from I got on MATLAB.
Just for some clarity, here is a side by side comparison of the expected result (from DESMOS, left pic) and the plotted result (from MATLAB, right pic).
It does appear that the complex values take it down to around 1.5 on the x-axis but it curves leftwards before doing so which isn't correct.
If you could help with that I'd greatly appreciate.
Thanks again.

Accedi per commentare.


Walter Roberson
Walter Roberson il 8 Nov 2021
Your syntax looks good.
I am concerned that the difference in starting value, 1.4 vs 1.2. Let's check,
a1 = 0.5;
n = 4;
S = 0.5;
K = S^n;
syms x1
x2 = ((K^2*(1-(x1))+K*(1+(a1)).*x1.^4-K*x1.^5)./(K*x1+x1.^5-a1.*x1.^4)).^(1/4);
limit(x2, x1, 0)
ans = 
NaN
vpa(solve(x2==1.4,x1))
ans = 
vpa(solve(x2==1.2,x1))
ans = 
So possibly the other one just plotted more densely.
fplot([real(x2),imag(x2)], [0 3])
ax = gca;
ax.XAxis.MinorTick = 'on';
ax.YAxis.MinorTick = 'on';
ax.XAxis.MinorTickValues = 0:.5/5:3;
ax.YAxis.MinorTickValues = 0:.2/5:1.4;
grid on
ax.XMinorGrid = 'on';
ax.YMinorGrid = 'on';
Looks okay. I think you were just seeing an artifact of not plotting densely enough.

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by