add / interpolate arrays of different length

1 次查看(过去 30 天)
Hello community,
i have two array's tsol and ysol. Each array has the length of 9;
I have also an error estimate array errorEst. The error estimate has only 4 elements.
The four elements have a "time" length of 0.25.
The errorEst
tsol = linspace(0,1,9);
ysol = sin(tsol*pi);
errorEst = rand(1,4); %each bar has a length of 0.25;
bar(errorEst);
plot([0 0.25 0.5 0.75; 0.25 0.5 0.75 1],[errorEst; errorEst]/0.25,'k')
title('gradient of errorEst ')
xlabel('t')
ylabel('d errorEst / dt')
% who could i integrate this gradient of errorEst w.r.t the time tSol and
% add to ysol?
% the first part should be a continous (function/plot)
% is there an elegant solution, which is parallelizable?
the solution should be plottet:
%yp = ysol + errorEstLin;
%ym= ysol -errorEstLin;

回答(1 个)

Tony
Tony 2024-5-17
Here's one way to do it by piece-wise integration by checking which interval you are.
tsol = linspace(0,1,9);
ysol = sin(tsol*pi);
errorEst = rand(1,4); %each bar has a length of 0.25;
bar(errorEst);
errorEst0 = 0; % need to specify initial error at t=0
errorEstLin = zeros(size(tsol));
for i = 1:numel(tsol)
if i == 1
errorEstLin(i) = errorEst0;
else
errorEstIntervalPrev = ceil(tsol(i-1) / 0.25 + 1e-12); %1e-12 to exclude lower bound of interval from interval
errorEstIntervalCurr = ceil(tsol(i) / 0.25);
errorEstLin(i) = errorEstLin(i - 1);
if errorEstIntervalCurr == errorEstIntervalPrev
errorEstLin(i) = errorEstLin(i) + (tsol(i) - tsol(i-1)) * errorEst(errorEstIntervalPrev);
else
errorEstLin(i) = errorEstLin(i) ...
+ (0.25 * errorEstIntervalPrev - tsol(i-1)) * errorEst(errorEstIntervalPrev) ...
+ sum(0.25 * errorEst(errorEstIntervalPrev + 1 : errorEstIntervalCurr - 1)) ...
+ (tsol(i) - 0.25 * (errorEstIntervalCurr - 1)) * errorEst(errorEstIntervalCurr);
end
end
end
yp = ysol + errorEstLin;
ym= ysol -errorEstLin;
plot(tsol', [ysol ; yp ; ym ; errorEstLin]')
legend({'ysol', 'yp', 'ym', 'errorEstLin'})

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by