function handle as boundary condition

4 次查看(过去 30 天)
sorry if this is a very silly question as I am quite new using matlab. So I apologise in advance.
I have a vector with accelerations corresponding to time intances. I need to use a interpolation function to interpolate values of accelerations at different times, and I need to pass this function as boundary condition in specific edges.
len=length(accel_filt);
%Create a time vector with the total length of the vector of acceleration
%measure every time instance
tlist=linspace(0,len,len);
%Create the interpolation function to use as boundary condition
bc = griddedInterpolant(tlist,accel_filt',"linear","nearest");
%--APPLY BOUNDARY CONDITION TO INNER EDGES-- 19/04/23----------------------
for j = 1:numel(edges) %for every value store in perimeter
edge=edges(j);
applyBoundaryCondition(modelTwoDomain,"dirichlet","Edge",edge,"u",@bcfundcD);
end
function defined at the end of code:
function bc=bcfundcD(location,bc)
state.u = bc(location.x);
end
What am I doing wrong?
  13 个评论
Jorge Garcia Garcia
Thanks!! will check it out. And thanks for all your guidance! Will let you know what happens :D
Jorge Garcia Garcia
编辑:Jorge Garcia Garcia 2023-4-26
Hello again
I have been some days trying the following:
tshort= tlist(1:2); %reduce the selection to 2 secs
tshort=tshort'; %transforms tlist into column vector to be like accel
ashort=accel_filt(1:2); %reduce acceleration to short time of test
%bcfund = @(location,state) [bc(state.time); 0];
bc = griddedInterpolant(tshort,ashort,"linear","nearest");
% Define the time at which to evaluate the boundary condition
t = 0.05;
% Evaluate the acceleration at time t
accel_bc = bc(t);
bcfund = @(location,state) [0;accel_bc];
% Apply the modified boundary condition to the PDE model
for j = 1:numel(edges)
edge = edges(j);
applyBoundaryCondition(modelTwoDomain, "dirichlet", "Edge", edge, "u", bcfund)
end
I think it kills matlab and it keeps executing but does not solve the system
res=solvepde(modelTwoDomain,tshort);
I am useless. Any clues?

请先登录,再进行评论。

回答(1 个)

Avni Agrawal
Avni Agrawal 2023-11-16
Hi,
I understand that you are trying to pass function handle as an argument to the ‘applyBoundaryCondition’ function in MATLAB. Here is an example of how you can achieve this:
Method 1:
bcfundcD = @(location, state) yourCustomBoundaryCondition(region, state);
% Apply the boundary condition using the function handle
applyBoundaryCondition(model, 'face', 1:4, 'u', bcfundcD);
‘yourCustomBoundaryCondition’ is a function that you have defined to implement your specific boundary condition logic. The bcfoundcD’ function handle is created using an anonymous function that calls ‘yourCustomBoundaryCondition’ with the appropriate arguments. i.e location, state.
Method 2:
% Define the named function
function bcfundcD = yourCustomBoundaryCondition(region, state)
% Implement your boundary condition logic here
% ...
end
% Apply the boundary condition using the function handle
applyBoundaryCondition(model, 'face', 1:4, 'u', @yourCustomBoundaryCondition);
In this case, the ‘@’ symbol is used to create a function handle to the named function ‘yourCustomBoundaryCondition’.
Take a look at this documentation for better understanding:
I hope this helps.

Community Treasure Hunt

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

Start Hunting!

Translated by