How to convert this function of one variable into MATLAB code?
显示 更早的评论
Hi I'm trying to convert this function
into MATLAB code.
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 个)
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')
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)
vpa(solve(x2==1.4,x1))
vpa(solve(x2==1.2,x1))
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




