# Thread: Matlab: Newton-Raphson Method

1. ## Matlab: Newton-Raphson Method

Hi guys, I need some help solving the following question using matlab: My Attempt:

$\displaystyle t_{2}$ is the time at which the object hits the ground (when the height =0, or the root/x-intercept of the graph).

I have to use this Newton Raphson formula:

$\displaystyle x^k = x^{k-1} - \frac{f(x^{k-1})}{f'(x^{k-1})}$

so here's my Matlab command:

Code:

theta=pi/4; v=10; g=9.81; a=0.1;
n_itn=3;
y(1)=0;
for k=1:n_itn
y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a)
y'=diff(y)
y(k+1)=y(k)-y(k)/y'(k)
end

But unfortunately I get the following error:

Code:
??? Error: File: Q1.m Line: 6 Column: 3
The expression to the left of the equals sign is not a valid target for an assignment.
I don't understand what's the problem with line 6. Can anyone help?

P.S.
Just to have a confirmation I already tried to estimate the root by ploting y versus t and I think the root is around 1.41, here are my commands:

Code:

theta=pi/4; v=10;  g=9.81;   a=0.1;
nfinal=2;
t=1:0.01:nfinal;
x=((1-exp(-a*t))/a)*v*cos(theta);
y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a);
plot(t,y,’+')

2. Originally Posted by Roam Hi guys, I need some help solving the following question using matlab: My Attempt:

$\displaystyle t_{2}$ is the time at which the object hits the ground (when the height =0, or the root/x-intercept of the graph).

I have to use this Newton Raphson formula:

$\displaystyle x^k = x^{k-1} - \frac{f(x^{k-1})}{f'(x^{k-1})}$

so here's my Matlab command:

Code:

theta=pi/4; v=10; g=9.81; a=0.1;
n_itn=3;
y(1)=0;
for k=1:n_itn
y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a)
y'=diff(y)
y(k+1)=y(k)-y(k)/y'(k)
end

But unfortunately I get the following error:

Code:
??? Error: File: Q1.m Line: 6 Column: 3
The expression to the left of the equals sign is not a valid target for an assignment.
I don't understand what's the problem with line 6. Can anyone help?

P.S.
Just to have a confirmation I already tried to estimate the root by ploting y versus t and I think the root is around 1.41, here are my commands:

Code:

theta=pi/4; v=10;  g=9.81;   a=0.1;
nfinal=2;
t=1:0.01:nfinal;
x=((1-exp(-a*t))/a)*v*cos(theta);
y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a);
plot(t,y,+')
diff only works on symbolic objects.

You need to differentiate y (wrt x) yourself.

CB

3. ## how do you define t? Originally Posted by CaptainBlack diff only works on symbolic objects.

You need to differentiate y (wrt x) yourself.

CB
I still have another problem!

Thanks for the hint, I differentiated it separately by creating a symbolic variable:

Code:

>> syms t

>> f=((1-exp(-0.1*t))/0.1)*105.17-((9.81*t)/0.1)

f =

10517/10-10517/10*exp(-1/10*t)-981/10*t

>> diff(f)

ans =

10517/100*exp(-1/10*t)-981/10

This is my new code:

Code:

theta=pi/4; v=10; g=9.81; a=0.1;
n_itn=3;
y(1)=0;
for k=1:n_itn
y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a)
z=10517/100*exp(-1/10*t)-981/10
y(k+1)=y(k)-y(k)/z(k);
end
I used "z" to denote $\displaystyle y'$ because for some reason matlab didn't accept "y'" in the code.

Now I get this error:

Code:

??? Undefined function or variable 't'.
Error in ==> Untitled at 5
y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a)
I don't understand... how do I need to define "t"?

4. Originally Posted by Roam I still have another problem!

Thanks for the hint, I differentiated it separately by creating a symbolic variable:

Code:

>> syms t

>> f=((1-exp(-0.1*t))/0.1)*105.17-((9.81*t)/0.1)

f =

10517/10-10517/10*exp(-1/10*t)-981/10*t

>> diff(f)

ans =

10517/100*exp(-1/10*t)-981/10

This is my new code:

Code:

theta=pi/4; v=10; g=9.81; a=0.1;
n_itn=3;
y(1)=0;
for k=1:n_itn
y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a)
z=10517/100*exp(-1/10*t)-981/10
y(k+1)=y(k)-y(k)/z(k);
end
I used "z" to denote $\displaystyle y'$ because for some reason matlab didn't accept "y'" in the code.

Now I get this error:

Code:

??? Undefined function or variable 't'.
Error in ==> Untitled at 5
y=((1-exp(-a*t))/a)*((g/a)+v*sin(theta))-((g*t)/a)
I don't understand... how do I need to define "t"?

Assign it a value t=10 (or something like that) first.

CB

5. Originally Posted by CaptainBlack Assign it a value t=10 (or something like that) first.

CB

I tried setting t=10 like you said & it's still not working and I keep getting errors. "??? Attempted to access y(2); index out of bounds because numel(y)=1.

Error in ==> Untitled at 8
y(k+1)=y(k)-y(k)/z(k);"

6. Originally Posted by Roam I tried setting t=10 like you said & it's still not working and I keep getting errors. Then abandon the use of symbolics and define the functions:

Code:
>>g=9.81;alpha=0.1;theta=pi/4;v=10;
>>
>>ff=@(t) (1-exp(-alpha*t))/alpha*(g/alpha+v*sin(theta))-g*t/alpha;
>>dff=@(t) exp(-alpha*t)*(sin(theta)*v+g/alpha)-g/alpha;
>>
>>t=10;
>>t=t-ff(t)/dff(t)
CB

#### Search Tags

matlab, method, newtonraphson 