Matlab

Moderators: None (Apply to moderate this forum)
Number of threads: 1086
Number of posts: 1767

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Evaluate Function using vectors Posted by soulbeginner on 4 Nov 2009 at 6:28 PM
How would I evaluate this using a row vector? (pref no loops! (its horrible!)
function [y, err] = arctanseries(x,n) -> x is the row vector, n is just an integer.

Thank you in advance to all who try!

function [y, err] = arctanseries(x,n)

if(x>=(-1))&(x<=1)
    power = [];
    nom = [];
    denom = [];
    neg = [];
    answer = [];
    
    % Vector neg, alternating values, 1 -1 1 -1 for arctanseries
    neg = ones(1,n);
    neg(2:2:end) = -1;
    
    % Vector power, starts at 1, increases by 2 for n-1 times
    power = 1:2:((2*n)-1);
    
    % Vector denom, denominator of the arctanseries. stats at 1
    denom = 1:2:((2*n)-1);
    
    % Vector nom, nominator equals x
    nom = (ones(1,n))*x;
    
    % solve arctan series 
    answer = nom .^ power;
    answer = answer .* neg;
    answer = sum(answer ./ denom);
    
    % ***Output***
    y = answer;
else
    err = error('Input Error: x must be between -1 and 1 inclusive')
end





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.