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

3 views (last 30 days)
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')

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 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 Comment
Angus K
Angus K on 8 Nov 2021
Edited: Stephen23 on 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.

Sign in to comment.


Walter Roberson
Walter Roberson on 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.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!