HELP!! Error: In an assignment A(I) = B, the number of elements in B and I must be the same.

조회 수: 5 (최근 30일)
Receiving this error :
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Homework (line 10) Error_abs(i) = abs(f_exact(x) - Taylor(x,i))
My two functions are 1)
function y = f_exact(x)
x = [1 2 8];
for n=1:1:10
y(n,:) = exp(-x/4);
end
end
and 2)
function y = Taylor(x,n)
f=1;
for i=1:10
ith_term=((f_exact(x)-0).^i)*((((-1/4).^i))/(factorial(i)));
f=f+ith_term
end
end
which work separately, however, I wish to implement them together and this is where i'm getting the error.
function Homework(x)
n = 10;
Error_abs = zeros(n,1);
epsilon = zeros(n,1);
Taylor = zeros(n,1);
for i= 1:1:n
Taylor(i) = Taylor(x,i)
Error_abs(i) = abs(f_exact{x} - Taylor(x,i)) <---- Error is in this line
epsilon(i) = abs(Error_abs(i) / f_exact(x))
end
end
Any tips on how to fix this problem would be greatly appreciated. Thank you!
  댓글 수: 1
Image Analyst
Image Analyst 2014년 6월 2일
Ben, you lost valuable time by not giving the full error message. You didn't copy the full error message so I don't know which of those dozens of lines of code caused your error. I'm signing off for the night - maybe tomorrow if no one has answered before then. Next time copy and paste ALL the red text - don't snip or paraphrase . In the meantime, please read this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

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

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 6월 2일
In the Homework function, there are two Taylors - the nx1 vector local variable
Taylor = zeros(n,1);
and the function
function y = Taylor(x,n)
This causes a conflict at the line where the function is called and the result is assigned to the vector
Taylor(i) = Taylor(x,i)
Variables and functions cannot have the same name. I recommend changing the local variable to
taylorVctr = zeros(n,1); % the nx1 vector
or removing it altogether since it doesn't seem to be used outside of the for loop.
Note that the Taylor function has a return value of y but never initializes this value, but there is an f that is summed at each iteration so this should be replaced with y (or vice versa) else there will be an error. Also the second input parameter n is unused. It may be that it should be used in the for loop
for i=1:n
as this would give results that show that the error is reduced between the exact function and the Taylor series approximation as the number of terms in the latter is increased. Further, y (or f) should be initialized as
y = f_exact(0);
rather than as 1 (even though it results in the same answer) just to be clear that our a or c is zero (is this true - that a is zero in this Taylor series expansion and so is the Maclaurin series?). And…I think that the exponent is incorrect on the first term. What we should have is the nth derivative evaluated at a (or c which is zero) which, because the function is exp(-x/4), is just f_exact(a) multiplied by (-1.4)^i. That product is then multiplied by (x-a)^i which is then divided by i!. What I think you really want for this code (based on http://en.wikipedia.org/wiki/Taylor_series) is:
function [y] = Taylor(x,n)
a = 0;
y = f_exact(a);
for i=1:n
% note that the ith derivative is f_exact(a)*(-1/4)^i
y = y + f_exact(a)*(-1/4)^i*(x-a)^i/factorial(i);
end
The function f_exact takes as an input parameter x and then immediately redefines it to a row vector with three elements. Why? I think that this line of code and the for loop should be removed as the output is a 10x3 matrix with each row being identical. If the idea is to have f_exact represent exp(-x/4), then that is all it should do:
function y=f_exact(x)
y = exp(-x/4);
end
unless there is some reason why x is replaced with a row vector and the for loop is there. Note that if this isn't changed, then there will be the error In an assignment A(I) = B, the number of elements in B and I must be the same. when assigning taylorVctr(i) = Taylor(x,i);.

추가 답변 (2개)

Star Strider
Star Strider 2014년 6월 2일
I haven’t run your code, but just on inspection you have at least two problems:
  1. Your ‘Taylor’ function does not generate any ‘y’ to return as an output;
  2. You are ‘shadowing’ your ‘Taylor’ function with your ‘Taylor’ vector.
There may be other problems, but fixing those (rename the vector ‘VecTaylor’ or something) will likely get you started. (I hope.)

Image Analyst
Image Analyst 2014년 6월 2일
Just spotted something. f_exact() is a function, so instead of f_exact{x}, use f_exact(x) - parentheses not braces.
  댓글 수: 1
Ben
Ben 2014년 6월 2일
Appreciate the response! However changing it to f_exact(x) still gives me the same error!

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by