Plotting of solutions using Newton Raphson method for a nonlinear function

13 views (last 30 days)
I need help while plotting a nonlinear equation using Newton Raphson method:
(x: solution to be calculated using NR)
plot(x,y)
a = 0:0.1:2;
fun = x^2 - 2*(a+x) + 4*a*x;
  2 Comments
Riccardo Scorretti
Riccardo Scorretti on 4 Apr 2022
Please clarify your question: Newton-Raphson method is used to solve (not to plot) a nonlinear equation. You can use the function plot to make a graphics of the function. By the way, you have a function with two variables (a and x). What do you want to do precisely?
Supriya Khatoniar
Supriya Khatoniar on 5 Apr 2022
Need to plot the solution of the above function after solving it by applying Newton Raphson method into it, for the ranges of values "a". I want to plot the solution of the function for each values of "a".
This is what I tried, but not sure enough:
clc; clear all; close all;
A=0:0.1:2;
syms x;
for i = 1:numel(A)
a = A(i);
fun = x^2 - 2*(a+x) + 4*a*x;
x0 = 0; %intial value
N = 100;
step = 1;
e= 0.00001;
dfun = diff(fun,x);
fa = eval(subs(fun,x,x0));
while abs(fa)> e
fa = eval(subs(fun,x,x0));
ga = eval(subs(dfun,x,x0));
b = x0 - fa/ga;
fprintf('step=%d\ta=%f\tf(a)=%f\n',step,x0,fa);
x0 = b;
step = step + 1;
X(i) = x0;
end
fprintf('Root is %f\n', x0);
end
plot(A,X);

Sign in to comment.

Answers (3)

KSSV
KSSV on 5 Apr 2022

Sam Chak
Sam Chak on 5 Apr 2022
Check if this script is helpful. You can adjust plot properties to suit your preferences.
function Demo_NR
close all
clc
x0 = 1;
epsilon = 1e-9;
n = 0:0.1:2;
x = zeros(1, length(n));
for a = 0:0.1:2
f = @(x) x^2 - 2*(a + x) + 4*a*x;
x = NewtonRaphson(f, x0, epsilon);
plot(a, x, '*', 'color', 'b')
hold on
end
grid on
xlabel('a');
ylabel('x');
title('Solutions via Newton–Raphson method')
print -f1 -dpng -r300 fig_Demo_NR.png
end
function [x, Iter] = NewtonRaphson(f, x0, epsilon)
Iter = 0;
df = @(x) (f(x+1.0e-6)-f(x))*1e6;
x1 = x0 - f(x0)/df(x0);
while abs(f(x1)) > epsilon
x0 = x1;
x1 = x0 - f(x0)/df(x0);
Iter = Iter + 1;
end
x = x1;
end
Result

Nada Ahmed
Nada Ahmed on 26 Apr 2022
help me .. i need question soultion matlab
Assignment (1)
1. Problem Description
The equation of state developed by Beattie and Bridgeman is an extension of the ideal gas law and is given by the following equation: 𝑃=𝑅𝑇𝑉+𝑎𝑉2+𝑏𝑉3+𝑐𝑉4
Where 𝑃 is the gas pressure in [atm], 𝑇 is temperature in [K], 𝑉 is volume in [L/mol], 𝑅 is the ideal gas constant in [atm∙L/mol∙K] and 𝑎, 𝑏 and 𝑐 are constants. For the conditions and constants given in the table below, use MATLAB to determine the volume (𝑉). Note that Eqn. 1 has multiple roots but the one you are looking for will fall between 0.001 and 0.01 L/mol.
Solve this problem using MATLAB and each of the following numerical methods to an error tolerance of s  = 1∗10−5 %.
• Bisection method
• False position method
• Newton-Raphson method
Script File:
A main script file must also be created (Lab1.m) that does the following:
• Creates all the necessary variables (parameters in Table).
• Creates an anonymous function for the non-linear equation and any other anonymous functions required in the function files.
• Creates variables for the error tolerance and the maximum number of iterations.
• Creates variables for the initial guesses for each numerical method.
• Calls each of the three function files (Bisection.m, FalsePosition.m, NewtRaph.m) to solve for the volume 𝑉. Use different variable names for the resulting volume, approximate error and number of iterations from each method such that these values will not be overwritten when another function is called.
Results
Include the following results/discussions in your report:
• A table presenting the results from each of the three numerical methods. The table should include the following results for each method:
➢ the initial guess(es) o the final volume 𝑉
➢ the number of iterations required for the method to converge
➢ the approximate percent relative error after convergence is reached
➢ You may use any software to create this table (ex. Microsoft Excel)
𝑃 [atm]
𝑇 [K]
𝑅 [atm∙L/mol∙K]
𝑎
𝑏
𝑐
25
293
0.08206
−0.106
0.057
−0.0001
Submission Requirements
o Each student must submit the report as a pdf document and the four MATLAB m-files (Lab1.m, Bisection.m, FalsePosition.m, NewtRaph.m)
o Please combine all the files mentioned above into a folder and compress the folder into a zip file.

Community Treasure Hunt

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

Start Hunting!