寻找柯西概率的解决方案.在Matlab中 [英] Finding solution to Cauchy prob. in Matlab
问题描述
在Matlab中寻找柯西问题的解决方案时,我需要一些帮助.
问题:
y''+ 10xy = 0,y(0)= 7,y'(0)= 3
我还需要绘制图形.
我写了一些代码,但是我不确定它是否正确.特别是在功能部分.
有人可以检查吗?如果不正确,我在哪里弄错了?
这是其他.m文件中的单独功能:
I need some help with finding solution to Cauchy problem in Matlab.
The problem:
y''+10xy = 0, y(0) = 7, y '(0) = 3
Also I need to plot the graph.
I wrote some code but, I'm not sure whether it's correct or not. Particularly in function section.
Can somebody check it? If it's not correct, where I made a mistake?
Here is separate function in other .m file:
function dydx = funpr12(x,y)
dydx = y(2)+10*x*y
end
主要:
%% Cauchy problem
clear all, clc
xint = [0,5]; % interval
y0 = [7;3]; % initial conditions
% numerical solution using ode45
sol = ode45(@funpr12,xint,y0);
xx = [0:0.01:5]; % vector of x values
y = deval(sol,xx); % vector of y values
plot(xx,y(1,:),'r', 'LineWidth',3)
legend('y1(x)')
xlabel('x')
ylabel('y(x)')
我得到这张图:
推荐答案
ode45
及其相关类仅用于求解形式为y' = ...
的一阶微分方程.如果要解决二阶微分问题,则需要做一些工作.
ode45
and its related ilk are only designed to solve first-order differential equations which are of the form y' = ...
. You need to do a bit of work if you want to solve second-order differential questions.
具体来说,您必须将问题表示为一阶微分方程的系统.您当前具有以下ODE:
Specifically, you'll have to represent your problem as a system of first-order differential equations. You currently have the following ODE:
y'' + 10xy = 0, y(0) = 7, y'(0) = 3
如果我们重新排列它以解决y''
,我们将得到:
If we rearrange this to solve for y''
, we get:
y'' = -10xy, y(0) = 7, y'(0) = 3
接下来,您将要使用两个变量...分别称为y1
和y2
,例如:
Next, you'll want to use two variables... call it y1
and y2
, such that:
y1 = y
y2 = y'
您为ode45
构建代码的方式,所指定的初始条件正是这样-使用y
进行的猜测及其一阶猜测y'
.
The way you have built your code for ode45
, the initial conditions that you specified are exactly this - the guess using y
and its first-order guess y'
.
取每边的导数可得出:
y1' = y'
y2' = y''
现在,做一些最终的替换,我们得到一阶微分方程的最终系统:
Now, doing some final substitutions we get this final system of first-order differential equations:
y1' = y2
y2' = -10*x*y1
如果您在查看时遇到困难,只需记住y1 = y
,y2 = y'
,最后是y2' = y'' = -10*x*y = -10*x*y1
.因此,您现在需要构建函数,使其看起来像这样:
If you're having trouble seeing this, simply remember that y1 = y
, y2 = y'
and finally y2' = y'' = -10*x*y = -10*x*y1
. Therefore, you now need to build your function so that it looks like this:
function dydx = funpr12(x,y)
y1 = y(2);
y2 = -10*x*y(1);
dydx = [y1 y2];
end
请记住,向量y
是一个两元素向量,分别表示在x
处指定的每个时间点的y
值和y'
的值.我还认为将其设为匿名功能会更清洁.它需要更少的代码:
Remember that the vector y
is a two element vector which represents the value of y
and the value of y'
respectively at each time point specified at x
. I would also argue that making this an anonymous function is cleaner. It requires less code:
funpr12 = @(x,y) [y(2); -10*x*y(1)];
现在继续解决(使用您的代码):
Now go ahead and solve it (using your code):
%%// Cauchy problem
clear all, clc
funpr12 = @(x,y) [y(2); -10*x*y(1)]; %// Change
xint = [0,5]; % interval
y0 = [7;3]; % initial conditions
% numerical solution using ode45
sol = ode45(funpr12,xint,y0); %// Change - already a handle
xx = [0:0.01:5]; % vector of x values
y = deval(sol,xx); % vector of y values
plot(xx,y(1,:),'r', 'LineWidth',3)
legend('y1(x)')
xlabel('x')
ylabel('y(x)')
请注意,通过deval
模拟微分方程的解时,输出将是两列矩阵.第一列是系统的解决方案,而第二列是该解决方案的派生词.这样,您将需要绘制第一列,这是plot语法正在做的事情.
Take note that the output when simulating the solution to the differential equation by deval
will be a two column matrix. The first column is the solution to the system while the second column is the derivative of the solution. As such, you'll want to plot the first column, which is what the plot syntax is doing.
我现在得到这个情节:
这篇关于寻找柯西概率的解决方案.在Matlab中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!