How to form a function using 'for loop' without 'sym' command

i tried like this
m = 150;
Aos = 40;
nAi = 0;
for i=1:2:m
nAi = @(phi)nAi+cos(i*phi);
end
nA = vpa((nAi+Aos), 4)
Iam getting this error
Operator '+' is not supported for operands of type 'function_handle'.
Error in ll (line 8)
nA = vpa(nAi+Aos, 4)

2 Commenti

You did not mention, what you try to achieve. All we see is the failing code. This is not enough information to guess, what you want to get instead.
Its clearly there. i want to form nA interms of phi.
But i want to evalute without sym. Any idea.....

Accedi per commentare.

Risposte (2)

Assuming you want nA be function of phi, you can use symsum for summuation :
m = 150;
Aos = 40;
syms phi k
nAi =@(phi) symsum(cos(2*k*phi),k,1,m/2);
nA =@(phi) vpa((nAi(phi)+Aos), 4);
% example
nA(pi)
ans = 
115.0

2 Commenti

syms is an abbreviation for sym() so this does not satisfy the requirement to not use sym
Oh! sorry, i just read the content not title of question.

Accedi per commentare.

m = 150;
Aos = 40;
nAi = @(phi) zeros(size(phi));
for i=1:2:m
nAi = @(phi)nAi(phi)+cos(i*phi);
end
nA = @(phi) nAi(phi)+Aos
nA = function_handle with value:
@(phi)nAi(phi)+Aos
You asked to avoid sym, so it seems likely to me you want to avoid using vpa() as well. But if using a final vpa is okay, then
vpa(nA, 4)
ans = 

2 Commenti

How to show result like this(ans which you shared) without vpa??
And you got result in the form of phi(means symbol), how to get symbol in place of phi??
With vpa(), the symbol for phi appears if you are using LiveScript, but not for traditional .m files.
To get the result without using vpa:
m = 150;
Aos = 40;
nA = str2func("@(phi) " + strjoin(compose("cos(%d*theta)", 1:2:m), " + ") + " + " + string(Aos))
nA = function_handle with value:
@(phi)cos(1*theta)+cos(3*theta)+cos(5*theta)+cos(7*theta)+cos(9*theta)+cos(11*theta)+cos(13*theta)+cos(15*theta)+cos(17*theta)+cos(19*theta)+cos(21*theta)+cos(23*theta)+cos(25*theta)+cos(27*theta)+cos(29*theta)+cos(31*theta)+cos(33*theta)+cos(35*theta)+cos(37*theta)+cos(39*theta)+cos(41*theta)+cos(43*theta)+cos(45*theta)+cos(47*theta)+cos(49*theta)+cos(51*theta)+cos(53*theta)+cos(55*theta)+cos(57*theta)+cos(59*theta)+cos(61*theta)+cos(63*theta)+cos(65*theta)+cos(67*theta)+cos(69*theta)+cos(71*theta)+cos(73*theta)+cos(75*theta)+cos(77*theta)+cos(79*theta)+cos(81*theta)+cos(83*theta)+cos(85*theta)+cos(87*theta)+cos(89*theta)+cos(91*theta)+cos(93*theta)+cos(95*theta)+cos(97*theta)+cos(99*theta)+cos(101*theta)+cos(103*theta)+cos(105*theta)+cos(107*theta)+cos(109*theta)+cos(111*theta)+cos(113*theta)+cos(115*theta)+cos(117*theta)+cos(119*theta)+cos(121*theta)+cos(123*theta)+cos(125*theta)+cos(127*theta)+cos(129*theta)+cos(131*theta)+cos(133*theta)+cos(135*theta)+cos(137*theta)+cos(139*theta)+cos(141*theta)+cos(143*theta)+cos(145*theta)+cos(147*theta)+cos(149*theta)+40

Accedi per commentare.

Prodotti

Release

R2021a

Richiesto:

il 6 Set 2021

Community Treasure Hunt

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

Start Hunting!

Translated by