フィルターのクリア

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 Center および File ExchangeNumerical 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