Trying to calculate root to an equation using newton raphson. also need to tabulate the results. the table isn't showing any info though.
clc
clear all
syms x
T = []; % Define table
x0 = .1; % Define Initial Value
i = 1; % Define Counter
N = 10; % Define max number of iterations
% Define Variables
S = 5281.716;
L = 5280;
f(x) = x - (S/L) * sin(x); % Define Function
df = diff(f); % Differential Function
while i <= N
x = x0 - (f(x0)/df(x0)); % Newton-Raphson Equation
if abs(x-x0) <= 10^-8
return
end
R = S/(2*x0); % Radius
d = R*(1-cos(x0)); % Distance d
T = [T; i x0 R d];
i = i + 1; % Increase Counter
x0 = x; % Update value of x0
vpa(x0) % Decimal
end
T = table(Iteration, Theta, Radius, d);

댓글 수: 5

John D'Errico
John D'Errico 2018년 8월 31일
Homework? If not, then USE FZERO!
Derek Reitnouer
Derek Reitnouer 2018년 8월 31일
have to use newton raphson method. problem is haven't used matlab in years.
John D'Errico
John D'Errico 2018년 8월 31일
You still have not answered my question. Is this homework? That is the only possible reason why you NEED to use Newton/Raphson, so the conclusion seems clear.
Derek Reitnouer
Derek Reitnouer 2018년 9월 2일
yes its a homework problem. I get the correct answer but can't figure out how to do the table and keep the answer to decimals.
Muhammad Qasim
Muhammad Qasim 2021년 7월 5일
cos(x)+2sin(x)+x^2

댓글을 달려면 로그인하십시오.

 채택된 답변

James Tursa
James Tursa 2018년 8월 31일

0 개 추천

Instead of doing all of this as symbolic, it would probably have been simpler to use function handles and work in double. That being said, maybe just convert your stuff to double as you accumulate it:
T = [T; double([i x0 R d])];
Then you can use T to plot stuff directly.
Also, it would make more programming sense to change that "return" statement to a "break" statement.

댓글 수: 1

Derek Reitnouer
Derek Reitnouer 2018년 9월 3일
I looked up the difference between return and break, thanks for the tip. I also looked at double and that makes sense. I want to name the columns on the table but it doesn't like this. Why?
clc clear all syms f(x)
T = []; % Define table x0 = .1; % Define Initial Value i = 1; % Define Counter N = 10; % Define max number of iterations
% Define Variables S = 5281.716; L = 5280;
f(x) = x - (S/L) * sin(x); % Define Function df = diff(f); % Differential Function
while i <= N x = x0 - (f(x0)/df(x0)); % Newton-Raphson Equation if abs(x-x0) <= 10^-8 break end R = vpa(S/(2*x)); % Radius d = vpa(R*(1-cos(x))) % Distance d T = [T; double([i x R d])] i = i + 1; % Increase Counter x0 = x; % Update value of x0 vpa(x0) % Decimal end T = table(Iteration, Theta, Radius, d);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Derek Reitnouer
Derek Reitnouer 2018년 9월 3일

0 개 추천

T.Properties.VariableUnits = {'Iteration' 'Theta' 'Radius' 'd'}; T
That doesn't work either.

태그

질문:

2018년 8월 31일

댓글:

2021년 7월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by