Matlab error (??? Error using ==> plot Conversion to double from function_handle is not possible.)?
-
i am very unfamiliar with matlab, my code is below. if you spot any errors it would really help me out. thank you in advance for the help. f = @(x) x.^3 - 3*x + 3; dfdx = @(x) 3*x.^2-3; max_iteration_count = 20; Xn = zeros(1,max_iteration_count + 1); FXn = zeros(1,max_iteration_count); dFXn = zeros(1,max_iteration_count); Xn(1) = -0.5; DX = 0.0001; plot_title = 'Steven Wallace x0=???'; my_xlim = [-2,2]; plot_to = max_iteration_count; for ind = 1:max_iteration_count FXn(ind) = f(Xn(ind)); dFXn(ind) = dfdx(Xn(ind)); if abs(dFXn) < eps plot_to = ind-1; fprintf('The derivative vanishes at iteration %li.\n Terminating the procedure./n',ind); break; else Xn(ind + 1) = Xn(ind) - FXn(ind)/dFXn(ind); if abs( Xn(ind+1) - Xn(ind) ) < DX plot_to = ind; break; end end end fprintf(' n Xn f(Xn) df/dx(Xn)\n') fprintf('--- --------- -------- ----------\n') for ind = 1:plot_to fprintf(' %1i %1.4f %1.4f %1.4f\n',ind-1,Xn(ind),FXn(ind),dFXn(ind... end fprintf(' %1i %1.4f -- --\n',plot_to,Xn(plot_to+1)); x = linspace(my_xlim(1),my_xlim(2),200); y = f; figure; plot(x,y,'k'); if plot_to > 0 hold on; for ind = 1:plot_to plot([Xn(ind),Xn(ind),Xn(ind+1)],[0,f(Xn... [ind/plot_to,0,1-ind/plot_to],'MarkerSiz... end hold off; else plot(Xn(1),f(Xn(1)),'or'); end grid on; xlim(my_xlim); set(gca,'FontSize',13) title(plot_title); xlabel('x'); ylabel('y');
-
Answer:
The problem is that you set y to a function, and try to plot it. You did the other calls before it correctly so I'm guessing you missed this one. Change the line "y = f;" to "y = f(x);" for the first plot. Since yahoo cuts off some of the lines, I can't interpret the line: plot([Xn(ind),Xn(ind),Xn(ind+1)],[ 0,f(Xn … [ind/plot_to,0,1-ind/plot_to], 'MarkerSiz Therefore I cannot see if there are any other errors, but the first one is fixed below. Code: fprintf(' %1i %1.4f -- --\n',plot_to,Xn(plot_to+1)); x = linspace(my_xlim(1),my_xlim(2),200); y = f(x); figure;
Michael at Yahoo! Answers Visit the source
Other answers
The problem is that you set y to a function, and try to plot it. You did the other calls before it correctly so I'm guessing you missed this one. Change the line "y = f;" to "y = f(x);" for the first plot. Since yahoo cuts off some of the lines, I can't interpret the line: plot([Xn(ind),Xn(ind),Xn(ind+1)],[ 0,f(Xn … [ind/plot_to,0,1-ind/plot_to], 'MarkerSiz Therefore I cannot see if there are any other errors, but the first one is fixed below. Code: fprintf(' %1i %1.4f -- --\n',plot_to,Xn(plot_to+1)); x = linspace(my_xlim(1),my_xlim(2),200); y = f(x); figure;
Michael
Related Q & A:
- How to draw this fractal using MATLAB?Best solution by Stack Overflow
- How to solve the “Your search cannot be completed because of a service error” error in Search Center?Best solution by SharePoint
- How to find the derivative without using a symbolic function in Matlab?Best solution by Stack Overflow
- How can I remove a picture's watermark using Matlab's image processing toolbox?Best solution by Yahoo! Answers
- Should I leave my higher paying (65K plus possible bonus) private sector job for a (62.5K) Federal government job?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.