Borrar filtros
Borrar filtros

I don't the error statement.

1 visualización (últimos 30 días)
Baris Menemenlioglu
Baris Menemenlioglu el 29 de Mzo. de 2022
Editada: Bhanu Prakash el 23 de Feb. de 2023
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 comentario
Dave B
Dave B el 29 de Mzo. de 2022
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)

Iniciar sesión para comentar.

Respuestas (1)

Bhanu Prakash
Bhanu Prakash el 15 de Feb. de 2023
Editada: Bhanu Prakash el 23 de Feb. de 2023
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.

Categorías

Más información sobre GPU Computing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by