What is wrong with this command line?

function y = cos_maclaurin(x,n)
%%This function will iterate through the values in the array x and for each
%%x-value it will perform a series summation for the cosine function using
%%n-terms in the series. The function will then output corresponding
%%y-values for each x-value.
y = 0;
for h = 0:n
a = (-1)^(h)*((x^(2*h))/(factorial(2*h)));
y = a + y;
end
line 3 = ???Undefined function or variable h

댓글 수: 3

Greg Heath
Greg Heath 2012년 9월 12일
Factorials take a lot of time for large n.
What is your error message?
Star Strider
Star Strider 2012년 9월 13일
편집: Star Strider 2012년 9월 13일
Well, for a start you didn't define n or x. When I defined n = 15 and x various values from (0 2*pi), it produced acceptable values for cos(x).
I assume that's what you want it to do.
Image Analyst
Image Analyst 2012년 9월 13일
편집: Image Analyst 2012년 9월 13일
Well, you don't need the semicolon at the end of the "for" line of code. And you have some unnecessary parentheses, and you have inconsistent use of spaces around mathematical operators. But none of those are really wrong - just a bit sloppy. If you were trying to do a Maclaurin series for cosine, see my demo below.

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

 채택된 답변

Matt Fig
Matt Fig 2012년 9월 12일
편집: Matt Fig 2012년 9월 13일

0 개 추천

Assuming x and n are defined before hand, there is nothing wrong with that command line. What makes you think something is wrong?
.
.
EDIT
How are you calling that function? Because when I call it I get no error.
>> cos_maclaurin(1.5,6)
ans =
0.0707372049851851
Also, if you really want to return the results for an array input, you should put your code in a loop (at the least - other improvements could be made):
function Y = cos_maclaurin(x,n)
% help goes here....
% n = n(1); % And other input checking goes here. Suggestions:
% isnumeric(x), isempty(x), same for n, etc.
Y = zeros(size(x));
for ii = 1:numel(x)
y = 0;
for h = 0:n
a = (-1)^(h)*((x(ii)^(2*h))/(factorial(2*h)));
y = a + y;
end
Y(ii) = y;
end
With the above code you can do:
>> cos_maclaurin([1.5 3],60)
ans =
0.0707372016677029 -0.989992496600446
Further enhancements might include the ability to enter a single n or an array n the same size as x, as well as efficiency improvements. Good luck!

댓글 수: 2

Walter Roberson
Walter Roberson 2012년 9월 13일
Assuming they are defined as scalars. There is a problem if x is a vector or matrix.
Matt Fig
Matt Fig 2012년 9월 13일
... or a cell array or a struct or..... True enough.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2012년 9월 13일

0 개 추천

Brian, I think this is what you were trying to achieve:
clc;
clearvars;
format compact
workspace;
y = 0;
numberOfTerms = 10;
index = 1;
for x = 0 : 0.1 : (3 * pi)
theSum = 0;
for h = 0 : numberOfTerms
thisTerm = (-1) ^ h * (x ^ (2 * h)) / factorial(2 * h)
theSum = theSum + thisTerm
end
y(index) = theSum;
index = index + 1;
end
x = 0 : 0.1 : (3 * pi);
plot(x, y, 'bo-', 'LineWidth', 2);
hold on;
plot(x, cos(x), 'r-', 'LineWidth', 2);
grid on;
title('Maclaurin Series for Cosine', 'FontSize', 20);
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
legend('Maclaurin Estimate', 'Cosine');
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

질문:

2012년 9월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by