如何在matlab中绘制该函数的图形?

发布于 2025-01-10 03:45:29 字数 741 浏览 0 评论 0原文

我有以下2n*π-周期函数F(x) = sin(x/n),我需要绘制dx/dt = γ - F(x) 位于从 02pi 的段上。所以它应该看起来像这个。我尝试用这种方式做matlab:

gamma = 1.01;
n=3;
[t,phi] = ode45(@(t,x)gamma-sin(x/n), [0,400], pi);
[t1,phi1] = ode45(@(t,x)gamma-sin(x/n), [112,400], 0);
[t2,phi2] = ode45(@(t,x)gamma-sin(x/n), [231,250], 0);
figure();  
plot(t, phi, 'k', t1, phi1, 'k', t2, phi2, 'k');
ylim([0 2*pi]);
yticks([0 pi 2*pi]);
yticklabels(["0" "\pi" "2\pi"]);
grid on; grid minor;
title('\itsin(x/n)')

但我只得到类似的东西。因此,这些行没有转移,而是“重新开始”。这里有人知道该怎么做吗?

I have the following 2n*π-periodic function F(x) = sin(x/n) and I need to graph the dx/dt = γ - F(x) on the segment from 0 to 2pi. So it should look like this. I tried to do it matlab this way:

gamma = 1.01;
n=3;
[t,phi] = ode45(@(t,x)gamma-sin(x/n), [0,400], pi);
[t1,phi1] = ode45(@(t,x)gamma-sin(x/n), [112,400], 0);
[t2,phi2] = ode45(@(t,x)gamma-sin(x/n), [231,250], 0);
figure();  
plot(t, phi, 'k', t1, phi1, 'k', t2, phi2, 'k');
ylim([0 2*pi]);
yticks([0 pi 2*pi]);
yticklabels(["0" "\pi" "2\pi"]);
grid on; grid minor;
title('\itsin(x/n)')

but I only got something like this. So there the lines are not transferred, but "begin anew". does anyone here know how to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2025-01-17 03:45:29

我得到一个类似于您的第一个草图的图,并根据您在评论中的代码(将来,将此类添加内容放入问题本身,使用格式将其标记为添加,然后在评论中引用它)并

  • 进行 更改pi 作为图中所示的初始点,
  • 使用 ODE 求解器的选项直接限制步长,并通过施加误差容限,
  • 您的原始时间跨度覆盖大约 3 个周期,将其减少到 [0, 200] 获得与绘图相同的特征。
gamma = 1.01; n=3; 

opts = odeset('AbsTol',1e-6,'RelTol',1e-9,'MaxStep',0.1); 
[t, phi] = ode45(@(t,x)gamma-sin(x/n), [0,200], pi, opts); 

phi = mod(phi, 2*pi); 

plot(t, phi, 'k'); 
ylim([0 2*pi]); yticks([0 pi 2*pi]); yticklabels(["0" "\pi" "2\pi"]); 
grid on; grid minor; 
title('\itsin(x/n)')

要获得更详细的信息,请使用事件获取数值解上恰好与 2*pi 周期相交的点,然后使用它来分割解图(样式省略)

function [ res, term, dir ] = event(t,y)
    y = mod(y+pi,2*pi)-pi;
    res = [ y ]; 
    dir = [1]; % only crossing upwards
    term = [0]; % do not terminate
end%function

opts = odeset(opts,'Events',@(t,y)event(t,y));

sol = ode45(@(t,x)gamma-sin(x/n), [0,200], pi, opts); 

tfs = [ sol.xe; sol.x(end) ]
N = length(tfs)
clf;
t0 = 0;
for i=1:N
    tf = tfs(i);
    t = linspace(t0+1e-2,tf-1e-2,150);
    y = deval(sol,t);  % octave: deval=@(res,t) interp1(res.x, res.y,t)
    y = mod(y,2*pi); 
    plot(t, y);
    hold on; 
    t0=tf;
end;
hold off;

在此处输入图像描述

I get a plot similar to your first sketch, and based on your code in the comments (in future, put such additions into the question itself, use formatting to mark it as addition, and cite it then in the comment) with the changes

  • use pi as initial point as seen in the drawing,
  • use the options of the ODE solver to restrict the step size, directly and by imposing error tolerances
  • your original time span covers about 3 periods, reduce this to [0, 200] to get the same features as the drawing.
gamma = 1.01; n=3; 

opts = odeset('AbsTol',1e-6,'RelTol',1e-9,'MaxStep',0.1); 
[t, phi] = ode45(@(t,x)gamma-sin(x/n), [0,200], pi, opts); 

phi = mod(phi, 2*pi); 

plot(t, phi, 'k'); 
ylim([0 2*pi]); yticks([0 pi 2*pi]); yticklabels(["0" "\pi" "2\pi"]); 
grid on; grid minor; 
title('\itsin(x/n)')

To get more elaborate, use events to get points on the numerical solution where it exactly crosses the 2*pi periods, then use that to segment the solution plot (styling left out)

function [ res, term, dir ] = event(t,y)
    y = mod(y+pi,2*pi)-pi;
    res = [ y ]; 
    dir = [1]; % only crossing upwards
    term = [0]; % do not terminate
end%function

opts = odeset(opts,'Events',@(t,y)event(t,y));

sol = ode45(@(t,x)gamma-sin(x/n), [0,200], pi, opts); 

tfs = [ sol.xe; sol.x(end) ]
N = length(tfs)
clf;
t0 = 0;
for i=1:N
    tf = tfs(i);
    t = linspace(t0+1e-2,tf-1e-2,150);
    y = deval(sol,t);  % octave: deval=@(res,t) interp1(res.x, res.y,t)
    y = mod(y,2*pi); 
    plot(t, y);
    hold on; 
    t0=tf;
end;
hold off;

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文