Why am I getting an error here?
5 views (last 30 days)
Show older comments
clear all
close all
clc
% constants and data
g = 9.81; D = 75; rho= 1000;
Z = 0:12.5:75;
w = [122 130 135 160 175 190 200];
%% Function handle for pressure, p(z)
p = @(z) rho*g*(D-z);
%% computing ft and d
I1 = @(z) rho*g*w(Z==z)*(D-z); % function inside ft integral
I2 = @(z) rho*g*z*w(Z==z)*(D-z); % function in the numerator of d
% evaluating the integrals
ft = simpson(I1,0,D) % integrate and get ft
d = simpson(I2,0,D)/simpson(I1,0,D) % integrate and divide to get d
%% function to compute the simpson's rule integral for function f on interval [a,b]
%
function I = simpson(f,a,b)
h = Z(2)-Z(1); % compute step size h
n = (b-a)/h; % compute number of intervals n
F = 0; % initialize the sum
for i = 0:n
if i==0 || i==n
c =1; % coefficient for first and last terms
elseif mod(i,2)~=0
c = 4; % coefficient for odd terms
elseif mod(i,2)==0
c = 2; % coefficient for even terms
end
F = F + c*f(a+i*h);
end
I = (h/3)*F; % final integral (sum)
end
end
0 Comments
Answers (1)
Walter Roberson
on 20 Mar 2023
function I = simpson(f,a,b)
depth 1
for i = 0:n
depth 2
if i==0 || i==n
depth 3
elseif mod(i,2)~=0
depth 3
elseif mod(i,2)==0
depth 3
end
depth 2
end
depth 1
end
depth 0 -- function has ended
end
depth negative 1. This end statement as no matching control structure
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices 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!