How to use an anonymous function in a ''for''?
Afficher commentaires plus anciens
Hi everyone!!
I'm trying to write a general solution for a differential equation using a moment method (Galerkin, with polynomials as base functions). When I try to write the general solution por N polynomials, the anonymous function that I'm using, it doesnt update with every loop. I write my code:
N = 10; %number of elements;
l = zeros([N,N]);
g = zeros(N,1);
A = zeros (N,1);
f = @(x) 0;
for i=1:N
for k = 1:N;
l(i,k) = i*k/(i+k+1);
g(k,1) = (k*(8+3*k))/(2*(2+k)*(4+k));
A = linsolve(l,g);
f = @(x) f(x) + A(i,1)*(x - x.^(i+1));
end
end
Thanks!!
3 commentaires
Star Strider
le 15 Oct 2020
First, this will fail because it uses ‘recursion’:
f = @(x) f(x) + A(i,1)*(x - x.^(i+1));
Second, what is the argument to the function supposed to be? It cannot be ‘x’ (as you called it with ‘f(x)’) because ‘x’ does not exist.
Migue Balma
le 15 Oct 2020
Steven Lord
le 15 Oct 2020
Use the sum function and element-wise operations inside the anonymous function.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!