I don't the error statement.

조회 수: 3 (최근 30일)
Baris Menemenlioglu
Baris Menemenlioglu 2022년 3월 29일
편집: Bhanu Prakash 2023년 2월 23일
clc;
clear;
close all;
Vmax = 50; %% m/s
R = 0.25; %% m
%% Equation is: v(r)= vmax*(R-r^2)^(cos(r)/8)
Vavg = @(r) 2/(R^2).*Vmax.*((R-r.^2)).^(cos(r)/8).*r; %% m/s %% function handle
Vavg = integral(Vavg,0,R); %% integral of V
Vavg_exact = 41.4592; %% m/s %% result from WolframAlpha
%% Compound Midpoint Method
% mp= (s(i)+s(i+1))/(2) %% eqn of midpoint
% h= s(i+1),-s(i)
dif = 1;
n = 1;
while dif > 0.01
s = linspace(0,R,n + 1);
out = 0; % holder
for i = 1:n
mp_approx = (2./(R^2).*Vmax.*(R-((s(i)+s(i+1))./2).^2).^(cos((s(i)+s(i+1))./2)/8).*((s(i)+s(i+1))./2)).*(s(i+1)-s(i));
out = out + mp_approx;
end
dif = abs(Vavg - out);
n = n + 1;
end
subp_mp = n - 1; % subpoint of midpoint approximation
%% Compound Trapezoidal Method
dif = 1;
n = 1;
while dif > 0.01
s = linspace(0,R,n + 1);
out = 0;
for i = 1:n
trap = ((2./(R^2).*Vmax.*((R-s(i).^2)).^(cos(s(i))/8).*s(i))+(2./(R^2).*Vmax.*(R-s(i+1).^2).^(cos(s(i+1))/8).*s(i+1))).*(s(i+1)-s(i))./2;
out = out + trap;
end
dif = abs(Vavg - out);
n = n + 1;
end
subp_trap = n - 1;
%% Simpson's Method
dif = 1;
n = 1;
while dif > 0.01
s = linspace(0,R,n + 1);
out = 0;
for i = 1:n
simp = ((2./(R^2).*Vmax.*((R-s(i).^2)).^(cos(s(i))/8).*s(i))+4*(2./(R^2).*Vmax.*(R-s(i+1).^2).^(cos(s(1+i))/8).*(s(1+i)))+(2./(R^2).*Vmax.*(R-s(2+i).^2).^(cos(s(2+i))/8).*s(2+i))).*(s(2+i)-s(i))./6;
out = out + simp;
end
dif = abs(Vavg - out);
n = n + 1;
end
subp_simp = n - 1;
The error i get is;
Index exceeds the number of array elements. Index must not exceed 2.
Error in BarisMenemenlioglu_HW4 (line 55)
simp = ((2./(R^2).*Vmax.*((R-s(i).^2)).^(cos(s(i))/8).*s(i))+4*(2./(R^2).*Vmax.*(R-s(i+1).^2).^(cos(s(1+i))/8).*(s(1+i)))+(2./(R^2).*Vmax.*(R-s(2+i).^2).^(cos(s(2+i))/8).*s(2+i))).*(s(2+i)-s(i))./6;
I don't really understand what's the problem.
  댓글 수: 1
Dave B
Dave B 2022년 3월 29일
you're referencing s(2+i)but you made s exactly n+1 elements big. You're looping until n, so when i is exactly n you're pointing to s(2+n) but s is n+1 elements big. Your index (2+i) exceeds the number of array elements (1+i)

댓글을 달려면 로그인하십시오.

답변 (1개)

Bhanu Prakash
Bhanu Prakash 2023년 2월 15일
편집: Bhanu Prakash 2023년 2월 23일
Hi Baris,
As per my understanding, you are trying to perform some analysis using Simpson’s method but are facing an error during execution.
The reason for the error could be improper indexing of the array “s”. The array “s” has only two elements in it and the code is trying to access the third element of this array, which is not present. So, it is throwing an error.
A possible solution to avoid this error is to change the parameters in “linspace” for “s” to contain three elements.
You can access the following documentation of “linspace”:
Hope this answer helps you.
Thanks,
Bhanu Prakash.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by