Error while vectorizing my code
Show older comments
x = 0:0.01:5; %% range of x values for graph
y = zeta_func(x);
plot(x,y),xlabel('x'), ylabel('zeta(x)'), title('Zeta(x) Graph'),
%% function definition
function zeta_val = zeta_func(x)
%% zeta(x) = summation of 1/x^n
%% here summing from n = 1 to 50
n = 1:1:50;
terms = 1./power(n,x); %% performing 1/x^i for all n
zeta_val = sum(terms); %% summing the values
end
I get the following error
Error using .^
Matrix dimensions must agree.
Error in zeta_gpu>zeta_func (line 11)
terms = 1./power(n,x); %% performing 1/x^i for all n
Error in zeta_gpu (line 2)
y = zeta_func(x);
Over here the function is being called on the whole array x. But I want to do element wise.
How do I acheive this over here?
Accepted Answer
More Answers (2)
terms = 1 ./ (n( : ) .^ x) % < 2016b 1 ./ bsxfun(@power, n( : ), x)
1 Comment
Mohsin Shaikh
on 28 Dec 2020
Luigi Emanuel di Grazia
on 27 Dec 2020
0 votes
Shouldn't it be power(x,n)?
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
