newton's method plotting

here is my code typed out. the hw prompt is below with pictures of my code in mat lab. i keep receiving an error message. once i get the plot to work, i will be able to input my guesses.
inside funq2.m
function (y)=funq2(x);
y=sin(x)-xcos(x);
end
inside citizenone.m
x=-3:0.01:3;
(y)=funq2(x);
plot(x,y)
grid on

5 Comments

For Newton's method you need your function and its derivative.
f=@(x)sin(x)-x.*cos(x);
fp=@(x)cos(x)+x.*sin(x)-cos(x);
Now write a loop using Newton's method stopping when you have enough accuracy.
Thank you. I am trying this now.
N/A
N/A on 8 Dec 2020
Edited: Walter Roberson on 11 Dec 2020
Is this what you meant? I think I put something in the wrong order. I tried to make this plot so I could use loops and make my guesses, yet this is not working.
inside funq2.m
function (y)=funq2(x);
y= sin(x)-x.*cos(x);
y'=x.*sin(x)
end
inside citizenone.m
x=-3:0.01:3;
(y)=funq2(x);
plot(x,y)
grid on
Justin comments:
I keep receiving emails to accept the answer for this question. The person did not answer it, I found the answer on my own. Please stop emailing me to accept an answer that was not an answer. Thank you.
Justin:
You could post the solution you found as an Answer and then Accept your Answer.

Sign in to comment.

Answers (1)

Something like this:
f=@(x)sin(x)-x.*cos(x);
fp=@(x)cos(x)+x.*sin(x)-cos(x);
x=-3:.01:3;
plot(x,f(x));
%% Newton's Method
X(1)=initialGuess;%provide an initial guess
error=1;
tol=1e-6;%provide a tolerance
count=1;
while error>tol
X(count+1)=X(count)-f(X(count))/fp(X(count));
error=abs(X(count+1)-X(count));%or however you want to determine the error
count=count+1;
end

3 Comments

N/A
N/A on 8 Dec 2020
Edited: Walter Roberson on 11 Dec 2020
Thank you. Here is what I did:
f=@(x)sin(x)-x.*cos(x);
fp=@(x)cos(x)+x.*sin(x)-cos(x);
x=-3:.01:3;
plot(x,f(x));
X(1)=initialGuess;
error=1;
tol=1e-6;
count=1;
while error>tol
X(count+1)=X(count)-f(X(count))/fp(X(count));
error=abs(X(count+1)-X(count));
count=count+1;
end
Since I see one x intercept, I input this into prompt command, yet I am not sure what to do here since your noatation is slightly different than what I am used to...
>> fzero('funq2',0)
That is the noatation I used on a similar assignment last week. Would I be done after I got the graph? I thought I was to receive an ans= in the command window.
You are suppose to use Newton's method to solve. Have you studied Newton's method? I suggest you look at it: https://en.wikipedia.org/wiki/Newton%27s_method
I did not provide a complete solution. You need to plug in your initial guess for the root. The final answer will be in the end of the X array.
Yes, I had it reviewed in my class, yet my teacher does not make sense. I will find a tutor. Thank you for your time.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

N/A
on 8 Dec 2020

Edited:

on 11 Dec 2020

Community Treasure Hunt

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

Start Hunting!