필터 지우기
필터 지우기

How to find the sum of values of a function at mid point of every subinterval?

조회 수: 3 (최근 30일)
I am trying to run this code but don't know how to write code for finding value of function F at mid point of subinterval say, if my interval is [ti, ti+1] for i=0,1,2, n. and need to find sum of functional value at every (ti +ti+1)/2. I need to do it for Rsum2 in this code.
clc; clear all; format long
%%%%%%%%%%%%%%%%%%%%%%%%%%
alpha=0.8;% fractional index
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TN=1 % time
N=10
T0=0
tau=TN/N
T=[T0:tau:TN]
X0=0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X(1)=X0
F =@(X,T)=X+T
for n=0:N-1
Rsum=0;
for j=1:1:n+2
Rsum=Rsum+2*F (j)
end
Rsum2=0;
for j=1:1:n+1
Rsum2=Rsum2+4*F (j+1/2)
end
X(n+2)=X(1) +(tau^alpha)*Rsum + (tau^alpha)*Rsum2
end

채택된 답변

Matt J
Matt J 2021년 3월 20일
편집: Matt J 2021년 3월 20일
If your function is vectorized, like in the following example, it is quite simple:
fun=@(tm) tm.^2+ sqrt(tm); %A vectorized function
n=8;
t=sort(rand(1,n)) %interval end points t(i)
t = 1×8
0.0157 0.1988 0.2703 0.3719 0.5549 0.6696 0.7340 0.7594
tmid = t(1:end-1)/2 +t(2:end)/2 %interval mid-points
tmid = 1×7
0.1073 0.2345 0.3211 0.4634 0.6122 0.7018 0.7467
result = sum(fun(tmid))
result = 6.3528
  댓글 수: 3
Matt J
Matt J 2021년 3월 22일
You're welcome, but if it works, please Accept-click the answer.
Mubashara Wali
Mubashara Wali 2021년 3월 22일
Matt J Actually its working when F is a function of T only, but having a problem when F is a function of two independent variables as I mentioned in my code that F =@(X,T)=X+T. I would really appreciate if you can help me in this regard.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by