MATLAB: Integral of equation with vectors

integrationMATLABvectors

clc; clear all;
%%Test of Intergral
t = (0:0.5:8); % Timestep and interval
h = 10; % Water height
H = 4; % Wave height
a = [0 -0.7 -1.4 -1.8 -2.0 -1.9 -1.5 -0.9 -0.2 0.5 1.1 1.6 1.9 1.9 1.6 1.1 0.4]; % accelerations
u = [2.6 2.4 1.9 1.1 0.1 -0.8 -1.6 -2.3 -2.6 -2.5 -2.1 -1. -0.4 0.5 1.4 2.1 2.5]; % Velocity
D = 2; % Diameter
syms x
f(x) = pi.*(D/2)^2.*x.*a+1/2.*u.*abs(u).*D.*x; % My Function
p = arrayfun(@(x)integral(f(x),0,10)); %<--- first try integral this from the range of 0 to 10
g(x) = integral(f,0,10); %<-- need to integral this from the range of 0 to 10

Best Answer

h = 10;
H = 4;
a = [0 -0.7 -1.4 -1.8 -2.0 -1.9 -1.5 -0.9 -0.2 0.5 1.1 1.6 1.9 1.9 1.6 1.1 0.4];
u = [2.6 2.4 1.9 1.1 0.1 -0.8 -1.6 -2.3 -2.6 -2.5 -2.1 -1. -0.4 0.5 1.4 2.1 2.5];
D = 2;
f = @(x,a,u) pi.*(D/2)^2.*x.*a+1/2.*u.*abs(u).*D.*x;
p = arrayfun(@(a,u)integral(@(x)f(x,a,u),0,10),a,u);
or try
h = 10;
H = 4;
a = [0 -0.7 -1.4 -1.8 -2.0 -1.9 -1.5 -0.9 -0.2 0.5 1.1 1.6 1.9 1.9 1.6 1.1 0.4];
u = [2.6 2.4 1.9 1.1 0.1 -0.8 -1.6 -2.3 -2.6 -2.5 -2.1 -1. -0.4 0.5 1.4 2.1 2.5];
D = 2;
f = @(x) pi.*(D/2)^2.*x.*a+1/2.*u.*abs(u).*D.*x;
p = integral(f,0,10,'ArrayValued',true);