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?
9 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 13 Feb 2022
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.
Answers (1)
Nipun
on 24 Jan 2024
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:
- 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;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.
- 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
0 Comments
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!