HELP!! Error: In an assignment A(I) = B, the number of elements in B and I must be the same.
Show older comments
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 Comment
Image Analyst
on 2 Jun 2014
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
Accepted Answer
More Answers (2)
Star Strider
on 2 Jun 2014
1 vote
I haven’t run your code, but just on inspection you have at least two problems:
- Your ‘Taylor’ function does not generate any ‘y’ to return as an output;
- 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
on 2 Jun 2014
0 votes
Just spotted something. f_exact() is a function, so instead of f_exact{x}, use f_exact(x) - parentheses not braces.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!