Why am I getting an error here?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Walter Roberson
am 20 Mär. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!