Errors using fzero in rootfinding
Show older comments
ecc= 0.1
M= linspace(0, 2*pi, 800);
%%solving for psi using fzero
f = @(psi) psi- ecc*sin(psi)- M;
psi = zeros(size(M));
for j = 1:length (M)
psi(j)=fzero(f,1);
end
The original equaions is M = ψ - epsilon* sin ψ, trying to solve psi(M) for each M entry.
Could someone spot the errors in this? When I run the program, it show erros in fzero , "operands to the || and && operators ust be convertible to logical scalar values. Errors in fzero (line 307) else if ~ isfinite(fx) || ~isreal f(x)
Answers (1)
Your problem is with how you set up your function. Your parameter M is a vector in the function definition, so at any given point, the function returns a vector. f(x) should return a scalar, not a vector. What your loop implies is that you're looking to find the root given M(j), so just do that.
ecc= 0.1
M= linspace(0, 2*pi, 800);
%%solving for psi using fzero
psi = zeros(size(M));
for j = 1:length (M)
f = @(psi) psi- ecc*sin(psi)- M(j);
psi(j)=fzero(f,1);
end
psi
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!