how do i make one function that i am able to use trapezoidal rule for integration from this code?
1 view (last 30 days)
Show older comments
x=0:0.01:25;
y=zeros;
for T1=1:length(x)
if x(T1)>=0 && x(T1)<5
y(T1)=0.1553567*(x(T1)^6)-2.0416*(x(T1)^5)+9.1837*(x(T1)^4)-14.829*(x(T1)^3)-1.3703*(x(T1)^2)+32.821*(x(T1))-1.3155;
hold on
elseif x(T1)>=5 && x(T1)<15.4
y(T1)=0.003980879*(x(T1)^5)-0.2247*(x(T1)^4)+4.8682*(x(T1)^3)-50.442*(x(T1)^2)+254.67*(x(T1))-430.66;\r\n
hold on
else ;x(T1)>=15.4;
y(T1)=-0.073*(x(T1)^2)+6.1802*(x(T1))+40.423;
end
end
5 Comments
Answers (1)
Star Strider
on 3 Nov 2018
One option:
yx = @(x) (0.1553567*(x.^6)-2.0416*(x.^5)+9.1837*(x.^4)-14.829*(x.^3)-1.3703*(x.^2)+32.821*(x)-1.3155).*((x>=0) & (x<5)) + (0.003980879*(x.^5)-0.2247*(x.^4)+4.8682*(x.^3)-50.442*(x.^2)+254.67*(x)-430.66).*((x>=5) & (x<15.4)) + (-0.073*(x.^2)+6.1802*(x)+40.423).*(x>=15.4);
x=0 : 0.01 : 25;
y_int1 = trapz(x, yx(x)) % Use ‘trapz’
y_int2 = integral(yx, 0, 25) % Use ‘integral’
y_int1 =
2457.58206158505
y_int2 =
2457.58239477744
0 Comments
See Also
Categories
Find more on Portfolio Optimization and Asset Allocation 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!