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

3 次查看(过去 30 天)
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')

回答(2 个)

Sulaymon Eshkabilov
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 个评论
Angus K
Angus K 2021-11-8
编辑:Stephen23 2021-11-8
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.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2021-11-8
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.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by