Why am I getting an error here?

1 次查看(过去 30 天)
Brendan
Brendan 2023-3-20
编辑: Torsten 2023-3-21
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
Function definitions in a script must appear at the end of the file.
Move all statements after the "simpson" function definition to before the first local function definition.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON

回答(1 个)

Walter Roberson
Walter Roberson 2023-3-20
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

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by