概述
MATLAB - 在ODE15s函数中生成方波信号?(MATLAB - Generate square signal in ODE15s function?)
我试图从函数内部生成一个方形信号,通过ODE15s求解器运行。 问题是,输出不是方形,而是线性的。
function dX = test(t ,X )
y = X (1) ;
a = 24;
dc = 50;
k = 2;
f=k*2*pi;
y=a*square(f*t,dc);
% derivative vector
dX = [ y ]';
运行脚本:
[t , X ]= ode15s ( @test ,[0 10] , [0]) ;
figure (1);
plot(t ,X)
有没有办法在ode函数中实现square函数? 我需要函数内部的值,因此不能从运行脚本中调用它,因为它是时间t的函数。
任何帮助,将不胜感激。 谢谢。
I'm trying to generate a square signal from inside a function, run through an ODE15s solver. The problem is, that the output is not square, but rather linear.
function dX = test(t ,X )
y = X (1) ;
a = 24;
dc = 50;
k = 2;
f=k*2*pi;
y=a*square(f*t,dc);
% derivative vector
dX = [ y ]';
The run script:
[t , X ]= ode15s ( @test ,[0 10] , [0]) ;
figure (1);
plot(t ,X)
Are there any way to implement a square function inside the ode function? I need the value inside the function, and therefore can't call it from the runscript, as it is a function of the time, t.
Any help would be appreciated. Thank you.
原文:https://stackoverflow.com/questions/30594151
更新时间:2020-01-15 14:38
最满意答案
部分答案:如果你放线
fprintf('%gt%gn', t, y)
在你的函数test ,你会看到解算器只在t y值为24的情况下调用函数:
0 24
0 24
0 24
1.57072e-14 24
1 24
2 24
3 24
4 24
5 24
6 24
7 24
8 24
9 24
10 24
我不知道为什么ode15s这样做。 使用ode45可以改善一些事情,但结果仍然不完美。
A partial answer: If you put the line
fprintf('%gt%gn', t, y)
into your function test, you will see that the solver calls the function only at values of t where y is 24:
0 24
0 24
0 24
1.57072e-14 24
1 24
2 24
3 24
4 24
5 24
6 24
7 24
8 24
9 24
10 24
I don't know why ode15s does that. Things can be a bit improved by using ode45 instead, but the result is still not perfect.
2015-06-03
相关问答
进行替换 z ≣ [x; y]
这样的 dz ≣ [dx; dy]
你实现这个 x0 = -1; %// Initial condition for variable x
y0 = -10; %// Initial condition for variable y
%// The ODEs. Note that z ≣ [x; y]
f = @(t,z) [
z(2) %// First ODE
z(2)+2*t]; %// Second ODE
...
如果我参考这篇文章: http : //www.mathworks.com/matlabcentral/newsreader/view_thread/307359 您无法指定最小步骤! 但是更改relTol和AbsTol的值将间接改变最小步骤。 祝好运! If I refer to this post: http://www.mathworks.com/matlabcentral/newsreader/view_thread/307359 You can't specify a minimum s
...
我已经解决了这个问题。 这是因为我的F(t,y)错了。 我只检查了jacobian然后我设法看到我有一些零行。 I've solved this problem. It was because my F(t,y) is wrong. I checked the jacobian only by then I managed to see that I have some lines of zeros.
根据文档, odefun传递给ode45 ,必须是一个函数句柄 。 解决ODE问题 y'= 2t 使用[0,5]的时间间隔和初始条件y0 = 0。 tspan = [0 5];
y0 = 0;
[t,y] = ode45(@(t,y) 2*t, tspan, y0);
@(t,y) 2*t返回匿名函数的函数句柄。 不幸的是, 函数句柄被列为MATLAB < - > Python转换中不支持的数据类型之一 : 不支持的MATLAB类型MATLAB Engine API for Python不支持以
...
部分答案:如果你放线 fprintf('%gt%gn', t, y)
在你的函数test ,你会看到解算器只在t y值为24的情况下调用函数: 0 24
0 24
0 24
1.57072e-14 24
1 24
2 24
3 24
4 24
5 24
6 24
7 24
8 24
9 24
10 24
我不知道为什么ode15s这样做。 使用ode45可以改善一些事情,但结果仍然不完美。 A partial answer: If you
...
短一:最大步长定义一次,可以在单个计算过程中使用标准MATLAB求解器进行更改。 很长一点:我很确定动态改变最大步长非常棘手而且没必要。 步长可以在最大值和最小值之间变化,这是我们从变量步长求解器得到的全部。 您可以尝试更改最小步长值 - 这种方法的想法是在低收敛的情况下自动调整步长。 此页面可以提供有关选择和调整求解器的一些有用信息。 分解子问题的问题可以减少时间更改步骤,但它会带来更多的麻烦而不是利润。 此外,我认为“临界点”可以在不同初始条件的时间线上移动。 最终建议包括两部分: 尝试按照M
...
我能想到的最简单的方法是将y设置为正弦波的符号,并在等于零时进行补偿。 我不知道C#是否有三重运算符,但是这样的东西可能会起作用: y[k] = Math.Sin(freq * k)>=0?A:-1*A;
The easiest way I can think of is to set y to be sign of a sine wave, making allowances for when it equals zero. I don't know if C# has the triple-
...
这是直接从fft文档调整,但使用方波作为信号: Fs = 1000; %// Sampling frequency
freq = 50;
T = 1/Fs; %// Sample time
L = 1000; %// Length of signal
t = (0:L-1)*T; %// Time vector
A = 10;
...
@ las3rjock: 它更像是对信号本身进行下采样,而不是FFT。请看下采样 。 或者您可以创建一个时间序列对象,并使用重新采样方法对其进行重新取样 。 编辑: 一个类似的例子:) % generate a signal
Fs = 200;
f = 5;
t = 0:1/Fs:1-1/Fs;
y = sin(2*pi * f * t) + sin(2*pi * 2*f * t) + 0.3*randn(size(t));
% downsample
n = 2;
yy = downsample
...
正如RM正确指出的那样 ,您在该代码中使用的是匿名函数 。 创建一个是通过以下方式完成的: fhandle = @(arglist) expr
其中arglist是用于计算函数表达式expr的输入参数列表。 您可能会感到困惑的是, 为什么代码要求创建匿名函数而不是仅使用现有函数的函数句柄 。 像ode15s和ode45这样的MATLAB求解器例程只会将两个输入传递给传递给它们的函数句柄:标量t和列向量y 。 如果您希望将更多参数传递给函数以定义其行为,则必须以参数化函数文档中所述的其他方式为函数
...
最后
以上就是沉静皮卡丘为你收集整理的matlab离散方波信号,MATLAB - 在ODE15s函数中生成方波信号?(MATLAB - Generate square signal in ODE15s function?)...的全部内容,希望文章能够帮你解决matlab离散方波信号,MATLAB - 在ODE15s函数中生成方波信号?(MATLAB - Generate square signal in ODE15s function?)...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复