I need help understanding one line of the code for a 2pbvp. Also, anyone knows if there are other methods of writing the solinit part of the code?

1 次查看(过去 30 天)
I have this example here but I have problems understanding the code. If some one can help explain that to me I'll greatly appreciate.
Also, are there any other ways of writing solinit for boundary value problems, I do not really udnerstand what is in the matlab help.
function er3OC_num2
solinit = bvpinit(linspace(0,1),[2;3;1;1],2); % I have problems understanding what is in []
sol = bvp4c(@ode, @bc, solinit);
y = sol.y;
time = sol.parameters*sol.x;
ut = -y(4,:);
figure(1);
plot(time,y([1 2],:)','-'); hold on;
plot(time, ut, 'k:');
axis([0 time(1,end) -1.5 3]);
text(1.3,2.5,'x_1(t)');
text(1.3,.9,'x_2(t)');
text(1.3,-.5,'u(t)');
xlabel('time');
ylabel('states');
title('Numerical solution');
hold off;
% -------------------------------------------------------------------------
% ODE's of augmented states
function dydt = ode(t,y,T)
dydt = T*[ y(2);
-y(4);
0;
-y(3) ];
% -------------------------------------------------------------------------
% boundary conditions: x1(0)=1;x2(0)=2, x1(tf)=3, p2(tf)=0;
% p1(tf)*x2(tf)-0.5*p2(2)^2
function res = bc(ya,yb,T)
res = [ ya(1) - 1;
ya(2) - 2;
yb(1) - 3;
yb(4);
yb(3)*yb(2)-0.5*yb(4)^2];
  3 个评论
Walter Roberson
Walter Roberson 2022-2-13
If you supply a vector of constants for the second parameter then the constants will be copied for all entries in the first parameter.
If you supply a function handle for the second parameter then it will be used to compute the initial values.

请先登录,再进行评论。

回答(1 个)

Nipun
Nipun 2024-1-24
Hi Rachel,
I understand that you intend to know more about the shared code snippet, especially the function "bvpinit".
The "bvpinit" function in MATLAB is used to create an initial guess for the solution of a BVP. Here is the link to documentation to acceptable syntaxes for the function in MATLAB: https://in.mathworks.com/help/matlab/ref/bvpinit.html#description
As per the shared code, there are three arguments to the "bvpinit" function:
  1. linspace(0,1): This creates a vector of points between 0 and 1, which will be used as the initial mesh points for the independent variable, typically representing time or spatial dimension.
  2. [2;3;1;1]:This is the initial guess column vector for the solution at each point in the mesh. Note that the number of elements in the column vector should match the number of ODEs for the system.
  3. 2: To solve the BVP, this is taken as an initial guess for parameter "tau" that is optimized as part of the solution.
Regarding an alternative for coding the "bvpinit" function: I suggest creating a structure that resembles the output of "solinit"
>> solinit = bvpinit(linspace(0,1),[2;3;1;1],2)
solinit =
struct with fields:
solver: 'bvpinit'
x: [0 0.0101 0.0202 0.0303 0.0404 0.0505 0.0606 0.0707 0.0808 0.0909 0.1010 0.1111 0.1212 0.1313 0.1414 0.1515 0.1616 0.1717 0.1818 0.1919 0.2020 0.2121 0.2222 0.2323 0.2424 0.2525 ]
y: [4×100 double]
parameters: 2
yinit: [4×1 double]
Here is the code to create a similar structure without using the function:
mesh_points = linspace(0, 1); % Create a vector of mesh points
initial_guess = [2; 3; 1; 1]; % Your initial guess for the state variables
solinit.x = mesh_points; % Independent variable mesh
solinit.y = repmat(initial_guess, 1, length(mesh_points)); % State variables initial guess at each mesh point
solinit.parameters = 2; % Initial guess for the parameters
solinit.yinit = initial_guess;
solinit.solver = 'bvpinit';
Hope this helps.
Regards,
Nipun

类别

Help CenterFile Exchange 中查找有关 Boundary Value Problems 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by