필터 지우기
필터 지우기

Arrays are Empty? WHY?

조회 수: 1 (최근 30일)
Alexa Shumaker
Alexa Shumaker 2019년 3월 24일
편집: Stephen23 2019년 3월 25일
Trying to do a math homework to make 1st order forward backward difference, 1 order forward difference, 2nd order forwad differnece, and 2nd order central difference.
Given x interval [-10,1-], distance between points h = 0.38. Was given formulas for SOFD,SOBD, and CD.
My Question, when I run the code my arrays are not being filled in the for loop.
L = -10; U = 10; h = 0.38;
x =L:h:U;
f= sech(x) ;
dfdt =(-sech(x).*tanh(x));
df2dt2 = sech(x).*(tanh(x).^2).*(sech(x).^3);
%f= function
%dfdt = 1st derivative
%df2dt2 = second derivative
%U = upper bound
%L = lower bound
% n = number of elements
% h = subinterval size
%CD= Centered Difference
%SOFD = Second order forward difference
%SOCD = Second Order Central Difference
%%
%Part A --> x = -10 to 10, h = 0.38
% array between upper and lower bounds via h different between points
n = length(x);
%prellocating for speed
x1p = zeros(1,n);
x2p = zeros(1,n);
x1m = zeros(1,n);
x2m = zeros(1,n);
f1p = zeros(1,n);
f2p = zeros(1,n);
f1m = zeros(1,n);
f2m = zeros(1,n);
CD= zeros(1,n);
SOFD = zeros(1,n);
SOBD= zeros(1,n);
eCD = zeros(1,n);
eSOFD = zeros(1,n);
eSOBD= zeros(1,n);
% for loop to solve all points for the give interval
for i = 1:n
% points of x with +1, +1, -2 and +2
x1p(i) = x(i) + x(i);
x2p(i) = x(i) + 2*x(i);
x1m(i) = x(i) - x(i);
x2m(i) = x(i) - 2*x(i);
% function of above x values based on +1, +1, -2 and +2
f1p(i) = f(x1p(i)); %f(x+1)
f2p(i) = f(x2p(i)); %f(x+2)
f1m(i) = f(x1m(i)); %f(x-1)
f2m(i) = f(x2m(i)); %f(x-2)
%Centered Difference
CD(i) = (f1p(i) - f1m(i))/ (2*x(i));
% Second Order Forward Difference
SOFD(i) = (-f2p(i) + 4*f1p(i) -3*f(x(i)))/(2*x(i));
% Second Order Backward Difference
SOBD(i) = (3*f(x(i)) - 4*f1m(i) + f2m(i))/(2*x(i));
% Calculating Error
if i ~=n
eCD(i) = abs((eCD(i-1)-eCD(i))/eCD(i-1))*100;
eSOFD(i) = abs((eSOFD(i-1)-eSOFD(i))/eSOFD(i-1))*100;
eSOBD(i) = abs((eSOBD(i-1)-eSOBD(i))/eSOBD(i-1))*100;
else, break,
end
end % end for loop
%%
%plotting
disp(eCD)
disp(eSOFD)
disp(eSOBD)
hold on
loglog(x,eSOFD,'r.-')
legend('Error SOFD')

답변 (3개)

Walter Roberson
Walter Roberson 2019년 3월 24일
I suspect you want f = @sech rather than f = sech(x) . You appear to be using f as a function inside your loop.
Your for loop at the end has
if i ~= n
else
break
end
but on the first iteration, i = 1 and that is not n, so the else break is going to happen, causing the for loop to exit.

Guillaume
Guillaume 2019년 3월 24일
My Question, when I run the code my arrays are not being filled in the for loop.
More to the point, when you run the code you get an error, something you haven't mentioned. So basically, your code doesn't run and it's no wonder nothing gets filled.
The error is Array indices must be positive integers or logical values and indeed xp1(i) is not a valid index into the vector f, since xp1 is not made of integers.
It's not clear what you're trying to do with the expressions
f1p(i) = f(x1p(i)); %f(x+1)
f2p(i) = f(x2p(i)); %f(x+2)
f1m(i) = f(x1m(i)); %f(x-1)
f2m(i) = f(x2m(i)); %f(x-2)
but none of them are valid.
By the way, x(i) + x(i), most people write that as 2*x(i), x(i) + 2*x(i) as 3*x(i) and of course x(i) - x(i) as 0 and x(i) - 2*x(i) as -x(i).

Alexa Shumaker
Alexa Shumaker 2019년 3월 24일
clear
clc
L = -10; U = 10; h = 0.38;
x =L:h:U;
f= sech(x) ;
dfdt =(-sech(x).*tanh(x));
df2dt2 = sech(x).*(tanh(x).^2).*(sech(x).^3);
%f= function
%dfdt = 1st derivative
%df2dt2 = second derivative
%U = upper bound
%L = lower bound
% n = number of elements
% h = subinterval size
%CD= Centered Difference
%SOFD = Second order forward difference
%SOCD = Second Order Central Difference
%%
%Part A --> x = -10 to 10, h = 0.38
% array between upper and lower bounds via h different between points
n = length(x);
%prellocating for speed
x1p = zeros(1,n);
x2p = zeros(1,n);
x1m = zeros(1,n);
x2m = zeros(1,n);
f1p = zeros(1,n);
f2p = zeros(1,n);
f1m = zeros(1,n);
f2m = zeros(1,n);
CD= zeros(1,n);
FOFD = zeros(1,n);
FOBD = zeros(1,n);
SOFD = zeros(1,n);
SOBD = zeros(1,n);
eCD = zeros(1,n);
eFOFD = zeros(1,n);
eFOBD= zeros(1,n);
eSOFD = zeros(1,n);
eSOBD= zeros(1,n);
% for loop to solve all points for the give interval
for i = 1:n
% points of x with +1, +1, -2 and +2
x1p(i) = x(i);
x2p(i) = 2*x(i);
x1m(i) = -x(i);
x2m(i) = -2*x(i);
% function of above x values based on +1, +1, -2 and +2
f1p(i) = sech(x1p(i)); %f(x+1)
f2p(i) = sech(x1p(i)); %f(x+2)
f1m(i) = sech(x1m(i)); %f(x-1)
f2m(i) = sech(x2m(i)); %f(x-2)
%Centered Difference
CD(i) = (f1p(i) - f1m(i))/ (2*h);
%First Order Forward Difference
FOFD(i) = (f1p(i) - sec(x(i)))/(h);
%First Order Backward Difference
FOBD(i) = (sec(x(i))-f1m(i))/(h);
% Second Order Forward Difference
SOFD(i) = (-f2p(i) + 4*f1p(i) -3*sec(x(i)))/(2*h);
% Second Order Backward Difference
SOBD(i) = (3*sec(x(i)) - 4*f1m(i) + f2m(i))/(2*h);
% Calculating Error
if i<= n
if i == 1; break
else
eCD(i) = abs((CD(i)-CD(i-1))/CD(i))*100;
eFOFD(i) = abs((FOFD(i)-FOFD(i))/FOFD(i))*100;
eFOBD(i) = abs((FOBD(i)-FOBD(i-1))/FOBD(i))*100;
eSOFD(i) = abs((SOFD(i)-SOFD(i))/SOFD(i))*100;
eSOBD(i) = abs((SOBD(i)-SOBD(i-1))/SOBD(i))*100;
end
else, break,
end
end % end for loop
%%
%plotting
hold on
first =(-sech(0)*tanh(0));
second = sech(0)*(tanh(0)^2)*(sech(0)^3);
loglog(x,(eSOFD),'--m', x,(eSOBD), 'g',x,(eCD), 'b', 0, first, 'rs',0, second,'bs')
legend('Error SOFD', 'Error SOBD', 'Error CD','df/dt,x = 0','df2/dt2, x=0');
%plot(x,eSOFD),'r.-')
%plot(x,eSOBD), 'g')
%plot(x,eCD), 'b')
%title('Comparisions')
%xlabel('X Axis')
%ylabel('Y Axis')
%Part c
%first = (-sech(0).*tanh(0));
%second =sech(0).*(tanh(0).^2).*(sech(0).^3);
%plot(x0,first, 'ro')
%plot(x0, second,'bo')
%legend('df/dt,x = 0','df2/dt2, x=0')
So I updated the code. It still doesn't fill in the arrays. I know the for loop finishes since my plot shows up but only the 1st position of array is filled the rest become zero, why is that?
  댓글 수: 1
Stephen23
Stephen23 2019년 3월 25일
편집: Stephen23 2019년 3월 25일
"So I updated the code."
Yes, but you need to understand Walter Roberson's answer, which explains why your loop never passes the first iteration. You are still making the same mistake here (combined with badly aligned code, which makes the code much harder to understand):
if i<= n
if i == 1; break
else
... your code
end
else, break,
end
"I know the for loop finishes since my plot shows up but only the 1st position of array is filled the rest become zero, why is that? "
Read Walter Roberson's answer, it explains why your break concept is buggy.
Solution: get rid of those pointless nested IF and ELSE calls and get rid of all of the counter-productive break calls that you are doing in your code, and replace them with one simple:
if i > 1
... your code ... NO BREAK!
end
It worked when I tried it.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by