# Primjene integrala

## Prosječna vrijednost funkcije

f(x):=3*(1-x)*sin(3*(x-0.5)^2)+0.2;

fp: 0.33043577433458$
p1:find_root(f(x)=fp,x,0.2,1)$
p2:find_root(f(x)=fp,x,0.5,0.8)$
p3:find_root(f(x)=fp,x,0.8,1)$
wxdraw2d(yrange=[-0.1,0.9],xrange=[-0.1,1.2],axis_left=false,axis_top=false,axis_bottom=false,
xtics={["a",0.2],["b",1]},ytics={["fp",fp]},axis_right=false,fill_color=aquamarine,color=black,filled_func=0,
explicit(min(fp,f(x)),x,0.2,1),fill_color="#d0ffff",filled_func=fp,explicit(f(x),x,0.2,1),filled_func=false,
xaxis=true,xaxis_type=solid,xtics_axis=true,ytics_axis=true,yaxis=true,yaxis_type=solid,proportional_axes=xy,
color=blue,explicit(f(x),x,0.2,1),color=black,parametric(0.2,t,t,0,f(0.2)),parametric(t,fp,t,0.2,1),
parametric(t,0,t,0,1.2),parametric(1,t,t,0,fp),line_type=dots,parametric(t,fp,t,0,0.2));

wxdraw2d(yrange=[-0.1,0.9],xrange=[-0.1,1.2],axis_left=false,axis_top=false,axis_bottom=false,
xtics={["a",0.2],["b",1]},ytics={["fp",fp]},axis_right=false,
fill_color=aquamarine,color=black,filled_func=fp,explicit(f(x),x,0.2,p1),explicit(f(x),x,p2,p3),
fill_color=light-salmon,explicit(f(x),x,p1,p2),explicit(f(x),x,p3,1),filled_func=false,
xaxis=true,xaxis_type=solid,xtics_axis=true,ytics_axis=true,yaxis=true,yaxis_type=solid,proportional_axes=xy,
color=blue,explicit(f(x),x,0.2,1),color=black,parametric(0.2,t,t,0,f(0.2)),parametric(t,fp,t,0.2,1),
parametric(t,0,t,0,1.2),parametric(1,t,t,0,fp),line_type=dots,parametric(t,fp,t,0,0.2)
);

### Zadatak

Temperatura zraka u Celzijevim stupnjevima u trenutku t zadana je funkcijom

f(t):=10+t/12+3*sin(%pi*(t-10)/12);

pri čemu je t iz [0,24] vrijeme u satima. Kolika je prosječna dnevna temperatura tog dana?

wxplot2d(f(t),[t,0,24],[y,0,15]);

integrate(f(t),t,0,24)/24;

float((f(7)+f(14)+2*f(21))/4);

## Izračunavanje volumena

draw3d(proportional_axes = 'xyz,surface_hide=false,xu_grid = 20,yv_grid = 50,colorbox=false,
    wired_surface=true,color=black,view = [60, 150],xrange = [0,3],yrange=[0,3],zrange=[0,4.5],
    axis_3d=false,enhanced3d=false,parametric(t,0,0,t,0,3),parametric(0,t,0,t,0,3),parametric(0,0,t,t,0,4.5),enhanced3d=[z,x,y,z],
    palette    = [-3,2,0],parametric_surface(cos(t)*cos(u)*(1 + 0.1*sin(3*t + 4*u)) + 1.5, sin(t)*cos(u)*(1 + 0.1*sin(t^2 - 2*u)) + 1.5, 
    2*sin(u)+ 2.5,t,0,%pi,u, 0,2*%pi),user_preamble="set label 'x' at 3,0.2,0; set label 'y' at 0,3.1,0; set label 'z' at 0,0.1,4.5; set view , ,1.1,1.1");

wxplot_size:[1024,768]$
h:0.1$
with_slider_draw3d(
   p,
   makelist(p,p,0.3,4.5,0.05),
    proportional_axes = 'xyz,surface_hide=true,xu_grid = 20,yv_grid = 50,colorbox=false,xyplane=-1,
    wired_surface=true,color=black,view = [60, 150],xrange = [0,3],yrange=[0,3],zrange=[-1,4.6],
    axis_3d=false,enhanced3d=false,parametric(t,0,0,t,0,3),parametric(0,t,0,t,0,3),parametric(0,0,t,t,0,4.5),
    line_width=2,color=blue,mesh([[0,0,p], [0,3,p]],[[3,0,p], [3,3,p]]),contour=both,contour_levels={p,4.498971,4.4989712,4.4989713,4.4989714,4.4989715},
    line_width=1,color=black,parametric_surface(cos(t)*cos(u)*(1 + 0.1*sin(3*t + 4*u)) + 1.5, sin(t)*cos(u)*(1 + 0.1*sin(t^2 - 2*u)) + 1.5, 
    2*sin(u)+ 2.5,t,0,%pi,u, 0,2*%pi),color=white,mesh([[1.5-h,1.5-h,0.01-1],[1.5-h,1.5+h,0.01-1]],[[1.5+h,1.5-h,0.01-1],[1.5+h,1.5+h,0.01-1]]),
    user_preamble="set label 'x' at 3,0.2,0; set label 'y' at 0,3.1,0; set view , ,1,1"
);

integrate(A(z),z,a,b);

f(x):=log(x+3/2);

wxplot_size:[600,400]$
wxdraw2d(yrange=[-0.1,1.5],xrange=[-0.1,3],axis_left=false,axis_top=false,axis_bottom=false,
xtics={["1/2",0.5],["5/2",2.5]},ytics=false,axis_right=false,filled_func=0,fill_color=aquamarine,
xaxis=true,xaxis_type=solid,xtics_axis=true,ytics_axis=true,yaxis=true,yaxis_type=solid,proportional_axes=xy,
explicit(f(x),x,0.5,2.5),color=blue,filled_func=false,explicit(f(x),x,0,3),color=black,parametric(0.5,t,t,0,f(0.5)),parametric(2.5,t,t,0,f(2.5)));

wxplot_size:[900,600]$
with_slider_draw3d(
   p,
   makelist(float(p),p,0,2*%pi,2*%pi/20),
   proportional_axes = 'xyz,surface_hide=false,xu_grid = 15,yv_grid =20,colorbox=false,
    palette=[27,2,-36],wired_surface=true,color=black,view = [60, 30],xyplane=0,xrange=[0,4],yrange=[-2.5,2.5],zrange=[-2.5,3.5],
    axis_3d=false,color=black,enhanced3d=false,parametric(t,0,0,t,0,4),parametric(0,t*sin(p),t*cos(p),t,0,2.5),
    enhanced3d=true,parametric_surface(x,f(x)*t*sin(p),f(x)*t*cos(p),x,0.5,2.5,t,0,1),yv_grid = round(20*p/(2*%pi))+2,
    enhanced3d=false,parametric_surface(x, f(x)*sin(t), f(x)*cos(t),x,0.5,2.5,t,0,p),
    parametric_surface(2.5, u*sin(t), u*cos(t),u,0,f(2.5),t,0,p),parametric_surface(0.5, u*sin(t), u*cos(t),u,0,f(0.5),t,0,p),
    user_preamble=concat("set label 'y' at -0.2,",1.8*sin(p),",",1.8*cos(p),"; set label 'x' at 3.8,0,-0.2; set view , ,2.7,2.7"));

draw3d(proportional_axes = 'xyz,surface_hide=false,xu_grid = 15,yv_grid = 20,colorbox=false,
    palette=[27,2,-36],wired_surface=true,color=black,view = [60, 30],xyplane=0,
    axis_3d=false,color=black,enhanced3d=false,parametric(t,0,0,t,0,4),parametric(0,0,t,t,0,2.5),
    enhanced3d=true,parametric_surface(x,0,f(x)*t,x,0.5,2.5,t,0,1),
    enhanced3d=true,parametric_surface(x, f(x)*sin(t), f(x)*cos(t),x,0.5,2.5,t,0,2*%pi),
    parametric_surface(2.5, u*sin(t), u*cos(t),u,0,f(2.5),t,0,2*%pi),parametric_surface(0.5, u*sin(t), u*cos(t),u,0,f(0.5),t,0,2*%pi),
    user_preamble="set label 'y' at -0.4,0,2.3; set label 'x' at 3.8,0,-0.3; set view , ,2,2");

%pi*integrate('f(x)^2,x,a,b);

%pi*integrate(f(x)^2,x,1/2,5/2);

float(%);

wxplot_size:[900,600]$
with_slider_draw3d(
   p,
   makelist(float(p),p,0,2*%pi,2*%pi/20),
   proportional_axes = 'xyz,surface_hide=false,xu_grid = 15,yv_grid = 20,colorbox=false,
   palette=[27,2,-36],wired_surface=true,color=black,view = [70, 30],xyplane=0,xrange=[-3.5,3.5],yrange=[-3.5,3.5],zrange=[-0.5,3],
   axis_3d=false,color=black,enhanced3d=false,parametric(t*cos(p),t*sin(p),0,t,0,3.5),parametric(0,0,t,t,0,3.5),
   enhanced3d=true,parametric_surface(x*cos(p),x*sin(p),f(x)*t,x,0.5,2.5,t,0,1),yv_grid = round(20*p/(2*%pi))+2,
   enhanced3d=false,parametric_surface(cos(t)*x, sin(t)*x, f(x),x,0.5,2.5,t,0,p),parametric_surface(cos(t)*2.5, sin(t)*2.5,u,u,0,f(2.5),t,0,p),
   parametric_surface(cos(t)*0.5, sin(t)*0.5,u,u,0,f(0.5),t,0,p),parametric_surface(cos(t)*u, sin(t)*u,0,u,0.5,2.5,t,0,p),
   user_preamble=concat("set label 'y' at -0.4,0,3.3; set label 'x' at ",3.3*cos(p),",",3.3*sin(p),",-0.3; set view , ,2.5,2.5"));

draw3d(proportional_axes = 'xyz,surface_hide=false,xu_grid = 15,yv_grid = 20,colorbox=false,
    palette=[27,2,-36],wired_surface=true,color=black,view = [70, 30],xyplane=0,
    axis_3d=false,color=black,enhanced3d=false,parametric(t,0,0,t,0,3.5),parametric(0,0,t,t,0,3.5),
    enhanced3d=true,parametric_surface(x,0,f(x)*t,x,0.5,2.5,t,0,1),
    enhanced3d=true,parametric_surface(cos(t)*x, sin(t)*x, f(x),x,0.5,2.5,t,0,2*%pi),parametric_surface(cos(t)*2.5, sin(t)*2.5,u,u,0,f(2.5),t,0,2*%pi),
    parametric_surface(cos(t)*0.5, sin(t)*0.5,u,u,0,f(0.5),t,0,2*%pi),parametric_surface(cos(t)*u, sin(t)*u,0,u,0.5,2.5,t,0,2*%pi),
    user_preamble="set label 'y' at -0.4,0,3.3; set label 'x' at 3.3,0,-0.3; set view , ,1.75,1.75");

2*%pi*integrate(x*'f(x),x,a,b);

2*%pi*integrate(x*f(x),x,1/2,5/2);

float(%);

## Izračunavanje duljine krivulje

f(x):=log(x)+cos(x)+2;

wxplot_size:[600,400]$
wxdraw2d(xrange=[-0.2,10],yrange=[-0.2,5.5],axis_left=false,axis_top=false,axis_bottom=false,
xtics={1,9},ytics=false,axis_right=false,xaxis=true,xaxis_type=solid,xtics_axis=true,ytics_axis=true,
yaxis=true,yaxis_type=solid,proportional_axes=xy,explicit(f(x),x,1,9),color=black,line_type=dots,
parametric(1,t,t,0,f(1)),parametric(9,t,t,0,f(9)));

with_slider_draw(n,makelist(n,n,1,30),
xrange=[-0.2,10],yrange=[-0.2,5.5],axis_left=false,axis_top=false,axis_bottom=false,
xtics=union({["1",1],["9",9]},setify(makelist(["",1+i*8/n],i,1,n-1))),ytics=false,axis_right=false,xaxis=true,
xaxis_type=solid,xtics_axis=true,ytics_axis=true,yaxis=true,yaxis_type=solid,proportional_axes=xy,
explicit(f(x),x,1,9),color=red,makelist(parametric(1+(i-1+t)*8/n,f(1+(i-1)*8/n)+(f(1+i*8/n)-f(1+(i-1)*8/n))*t,t,0,1),i,1,n),
color=black,line_type=dots,makelist(parametric(1+i*8/n,t,t,0,f(1+i*8/n)),i,0,n));

wxdraw2d(xrange=[-0.1,10],yrange=[-0.1,5.5],axis_left=false,axis_top=false,axis_bottom=false,
xtics={["a=x_0",1],["x_4=b",9],["x_1",3],["x_2",5],["x_3",7]},ytics=false,axis_right=false,xaxis=true,
xaxis_type=solid,xtics_axis=true,ytics_axis=true,yaxis=true,yaxis_type=solid,proportional_axes=xy,
explicit(f(x),x,1,9),color=black,parametric(t,f(1)+(f(3)-f(1))*(t-1)/2,t,1,3),parametric(t,f(3)+(f(5)-f(3))*(t-3)/2,t,3,5),
parametric(t,f(5)+(f(7)-f(5))*(t-5)/2,t,5,7),parametric(t,f(7)+(f(9)-f(7))*(t-7)/2,t,7,9),line_type=dots,
parametric(1,t,t,0,f(1)),parametric(9,t,t,0,f(9)),parametric(3,t,t,0,f(3)),parametric(5,t,t,0,f(5)),parametric(7,t,t,0,f(7)));

sum(sqrt((x[k]-x[k-1])^2+('f(x[k])-'f(x[k-1]))^2),k,1,n);

'f(x[k])-'f(x[k-1])='diff('f(c[k]),x)*(x[k]-x[k-1]);

sum((x[k]-x[k-1])*sqrt(1+('diff('f(c[k]),x))^2),k,1,n);

integrate(sqrt(1+'diff('f(x),x)^2),x,a,b);

integrate(sqrt(1+diff(f(x),x)^2),x,1,9);

quad_qag(sqrt(1+diff(f(x),x)^2),x,1,9,1);

### Površina kruga i duljina kružnice

f(x):=sqrt(r^2-x^2);

assume(r>0);

2*integrate(f(x),x,-r,r);

2*integrate(sqrt(1+diff(f(x),x)^2),x,-r,r);