how do i make one function that i am able to use trapezoidal rule for integration from this code?

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

When you edit away your question, you insult the person who made the effort to answer your question.
Stop closing the question. We will just keep reopening it. If you felt it was important enough to ask, then someone else might learn something from it too. If you are worried that your teacher will read what you asked, then you should never have asked the question in a public forum.

Sign in to comment.

Answers (1)

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

Tags

No tags entered yet.

Asked:

on 3 Nov 2018

Edited:

on 26 Nov 2018

Community Treasure Hunt

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

Start Hunting!