簡體   English   中英

如何在matlab中繪制function?

[英]How to graph the function in matlab?

我有以下2n*π -periodic function F(x) = sin(x/n)並且我需要繪制從02pi的線段上的dx/dt = γ - F(x) 所以它應該看起來像這樣 我試着這樣做 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)')

但我只有這樣的東西 所以那里的台詞沒有轉移,而是“重新開始”。 這里有人知道該怎么做嗎?

我得到一個 plot 類似於你的第一個草圖,並根據你在評論中的代碼(將來,將這些添加到問題本身,使用格式將其標記為添加,然后在評論中引用它)與變化

  • 如圖所示,使用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周期的點,然后使用它來分割解決方案 plot(省略樣式)

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;

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM