Finding solution to Cauchy prob. in Matlab

穿精又带淫゛_ 提交于 2019-11-27 08:27:55

问题


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

Main:

%% 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)')

I get this graph:


回答1:


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.

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

If we rearrange this to solve for y'', we get:

y'' = -10xy, y(0) = 7, y'(0) = 3

Next, you'll want to use two variables... call it y1 and y2, such that:

y1 = y
y2 = 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'.

Taking the derivative of each side gives:

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

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

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)')

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.

I get this plot now:



来源:https://stackoverflow.com/questions/34273482/finding-solution-to-cauchy-prob-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!