Understand fsolve in matlab?
Show older comments
Hi, I have an equation along the lines of f(x,k)=sinx-kx=0, and am told to use inputs k, to return x0(k). Is someone please able to explain how I could use fsolve, or any other method to find this. I am struggling to see how to input a k using this function. Thanks
1 Comment
Matt J
on 23 Nov 2014
So equivalently, you want to solve
sinc(x)=k
The equation has no solution for abs(k)>=1. For abs(k)<1, it will have multiple solutions. For k=0, it will have infinite solutions. Which ones do you want?
Answers (1)
Star Strider
on 22 Nov 2014
I am not sure what you want to do, but this will get you started:
fn = @(x,k) sin(x) - k*x; % Define Function
k = 0.5; % Define ‘k’ For This Solution
X0 = 2; % Initial Estimate For ‘x’
x = fzero(@(x) fn(x,k), X0); % Solve for ‘x’
Note that the solution for ‘x’ depends also on ‘X0’.
Experiment with this to get the result you want.
2 Comments
Hannah
on 22 Nov 2014
Star Strider
on 22 Nov 2014
You can change anything you like!
I would put this in a script instead of running it from the Command Window.
If you want to evaluate your function for a range of ‘k’ values, a loop is an option.
For example:
fn = @(x,k) sin(x) - k*x; % Define Function
k = 0.1:0.1:10; % Define A Range For ‘k’
for k1 = 1:length(k)
X0 = 2; % Initial Estimate For ‘x’
x(k1) = fzero(@(x) fn(x,k(k1)), X0); % Solve for ‘x’
end
The ‘x’ vector now has a solution for each value of ‘k’.
Categories
Find more on Solver Outputs and Iterative Display 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!