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

조회 수: 1 (최근 30일)
Angus K
Angus K 2021년 11월 8일
답변: Walter Roberson 2021년 11월 8일
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
Sulaymon Eshkabilov 2021년 11월 8일
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