Effacer les filtres
Effacer les filtres

The code will not run when I tried to write the code successfully, so try fixing the code, please.

1 vue (au cours des 30 derniers jours)
Problem 1:
Find the solutions to the equation:
xlog(x)=3
Part A.
Plot the appropriate function and find initial estimates, or intervals, for the location of the solution.
% Your code goes here:
% Define the function
f = @(x)x.*log(x) - 3;
% Plot the function
x = linspace(0.5, 10, 100);
y = f(x);
plot(x, y);
grid on;
Part B
Write a script that uses the Bisection method to find the solution to the equation.
% Your code goes here:
% Bisection method function
a = 0.5;
b = 1;
for n=1:20
c=(a+b)/2; % Bisection
if abs(f(c))<1.e-10
a=c;b=c; break
elseif f(a)*f(c)<0
b=c;
else
a=c;
end
end
c=(a+b)/2
c = 1.0000
c = 1.0000
c = 1
function x = Bisection(f, a, b, ~)
error = 1e-4;
% Check if the interval contains a root
if f(a)*f(b) > 0
error('The interval does not contain a root.');
end
% Initialize the midpoint
x = (a + b)/2;
% Iterate until the error is within tolerance
while abs(f(x)) > error
if f(x)*f(a) < 0
b = x;
else
a = x;
end
x = (a + b)/2;
end
% Display the solution
fprintf('The solution to the equation is x = %f.\n', x);
Part C Use the appropiate MATLAB function to solve the same problem and verify you solutions in Part B.
% Your code goes here:
% Use the fzero function to solve the equation
x = fzero(f, [0.5, 1]);
% Display the solution
fprintf('The solution to the equation is x = %f.\n', x);
Problem 2:
Find the solutions to the equation:
sin(x)+1x+1=5
Part A.
Plot the appropriate function and find initial estimates, or intervals, for the location of the solutions.
% Your code goes here:
% Define the function
f = @(x) sin(x) + 1/(x + 1) - 5;
fplot(f,[-2,2]);grid on
% Bisection
a=0;b=5; %c=0.2608
a=0;b=5; %c=0.9349
a=1.2;b=2;
for n=1:30
c=(a+b)/2; % Bisection
if abs(f(c))<1.e-10
a=c;b=c; break
elseif f(a)*f(c)<0
b=c;
else
a=c;
end
end
c=(a+b)/2
xsoln=fzero(f,[0,2])
xsoln=fzero(f,[0,5])
xsoln=fzero(f,[0,2])
hold on;plot(c,f(c),'*r');hold off
Part B
Write a script that uses the False Position method to find the solutions to the equation.
% Your code goes here:
% Your code goes here
clear
f = @(x) sin(x) + 1/(x + 1) - 5;
fplot(f,[1.2,2]);grid on
% Bisection
a=0;b=0.5; %c=0.2608
a=0.5;b=1.2; %c=0.9349
a=1.2;b=2;
for n=1:80
%c=(a+b)/2; % Bisection
m=(f(b)-f(a))/(b-a); % False Position
c=a-f(a)/m;
if abs(f(a)*f(c))<1.e-10
a=c;b=c; break
elseif f(a).*f(c)<0
b=c;
else
a=c;
end
end
c=(a+b)/2
disp([a,b])
xsoln=fzero(f,[0,0.5])
xsoln=fzero(f,[0.5,1.2])
xsoln=fzero(f,[1.2,2])
hold on;plot(c,f(c),'*r');hold off
Part C
Use the appropiate MATLAB function to solve the same problem and verify you solutions in Part B.
% Your code goes here:
% Use the fzero function to solve the equation
x = fzero(f, [-10, -1]);
% Display the solution
fprintf('The solution to the equation is x = %f.\n', x);
Problem 3:
Find the solutions to the equation:
x(log(x)+x)=5
Part A.
Plot the appropriate function and find initial estimates, or intervals, for the location of the solution.
% Your code goes here:
clc;clear all;close all;
%Part A
f=@(x)x.*(log(x)+x)-5;
df=@(x)log(x)+2+2*x;
x=1:0.01:3;
plot(x,f(x))
Part B
Write a script that uses the Newton's method to find the solution to the equation.
% Your code goes here:
%Part B
f=@(x)x.*(log(x)+x)-5;
df=@(x)log(x)+2+2*x;
es=0.001;
x=2;
x=Newton(f,df,x,es);
function x=Newton(f,df,x0,es)
while (1)
x=x0-f(x0)/df(x0);
if abs((x-x0)/x)*100<es
break
end
x0=x;
end
end
Part C
Use the appropiate MATLAB function to solve the same problem and verify you solutions in Part B.
% Your code goes here:
%part C
X=fzero(f,2);
Problem 3:
Find the solutions to the system of equations:
(x+1)2+(2y1)2=3
(x1)2+(y1)2xy=2
Part A.
Plot the appropriate function and find initial estimates, or intervals, for the location of the solutions.
% Your code goes here:
f1 = @(x, y) (x + 1)^2 + (2*y - 1)^2 - 3;
f2 = @(x, y) (x - 1)^2 + (y - 1)^2 - x*y - 2;
% Create a grid of x and y values for plotting
[x, y] = meshgrid(-2:0.1:2, -2:0.1:2);
% Calculate the values of the functions over the grid
z1 = f1(x, y);
z2 = f2(x, y);
% Create contour plots to visualize the functions
figure;
contour(x, y, z1, [0 0], 'r', 'LineWidth', 2);
hold on;
contour(x, y, z2, [0 0], 'b', 'LineWidth', 2);
xlabel('x');
ylabel('y');
title('Contour Plots of the Equations');
legend('f1(x, y) = 0', 'f2(x, y) = 0');
grid on;
Part B
Write a script that uses the Newton's method to find the solutions to the equation.
% Your code goes here:
% Define the system of equations as functions
f1 = @(x, y) (x + 1)^2 + (2*y - 1)^2 - 3;
f2 = @(x, y) (x - 1)^2 + (y - 1)^2 - x*y - 2;
% Define the Jacobian matrix of the system
Jacobian = @(x, y) [2*(x+1), 4*(2*y-1); 2*(x-1) - y, 2*(y-1) - x];
% Define tolerance and maximum number of iterations
tolerance = 1e-6;
maxIterations = 100;
% Initial guess
x0 = [0; 0];
% Initialize variables
x_k = x0;
for k = 1:maxIterations
% Calculate the values of the functions and Jacobian at x_k
f_k = [f1(x_k(1), x_k(2)); f2(x_k(1), x_k(2))];
J_k = Jacobian(x_k(1), x_k(2));
% Newton's method update
delta_x = -J_k\f_k;
x_k = x_k + delta_x;
% Check for convergence
if norm(delta_x) < tolerance
break;
end
end
% Display the solution
fprintf('Approximate Solution: x = %.6f, y = %.6f\n', x_k(1), x_k(2));
Part C
Use the appropiate MATLAB function to solve the same problem and verify you solutions in Part B.
% Your code goes here:
% Define the system of equations as a function handle
fun = @(x) [f1(x(1), x(2)); f2(x(1), x(2))];
% Initial guess
x0 = [0; 0];
% Solve the system of equations using fsolve
options = optimoptions('fsolve', 'Display', 'off');
[x_fsolve, ~, exitflag] = fsolve(fun, x0, options);
if exitflag > 0
fprintf('Solution found using fsolve: x = %.6f, y = %.6f\n', x_fsolve(1), x_fsolve(2));
else
fprintf('fsolve did not converge to a solution.\n');
end
% Compare with the solution from Part B
fprintf('Solution from Part B: x = %.6f, y = %.6f\n', x_k(1), x_k(2));
Problem 4:
Find the solutions to the system of equations:
(x+1)2+y=5
(x1)2+(y1)2sin(x)=2Part A.
Plot the appropriate function and find initial estimates, or intervals, for the location of the solutions.
% Your code goes here:
% Define the equations
f1 = @(x, y) (x + 1)^2 + y - 5;
f2 = @(x, y) (x - 1)^2 + (y - 1)^2 - sin(x) - 2;
% Create a grid of points for visualization
[x, y] = meshgrid(-5:0.1:5, -5:0.1:5);
% Calculate the values of the functions over the grid
z1 = f1(x, y);
z2 = f2(x, y);
% Create contour plots to visualize the functions
figure;
contour(x, y, z1, [0 0], 'r', 'LineWidth', 2);
hold on;
contour(x, y, z2, [0 0], 'b', 'LineWidth', 2);
xlabel('x');
ylabel('y');
title('Contour Plots of the Equations');
legend('f1(x, y) = 0', 'f2(x, y) = 0');
grid on;
Part B
Write a script that uses the Newton's method to find the solutions to the equation.
% Your code goes here:
f1 = @(x, y) (x + 1)^2 + y - 5;
f2 = @(x, y) (x - 1)^2 + (y - 1)^2 - sin(x) - 2;
% Define the Jacobian matrix of the system
Jacobian = @(x, y) [2*(x+1), 4*(2*y-1); 2*(x-1) - y, 2*(y-1) - x];
% Define tolerance and maximum number of iterations
tolerance = 1e-6;
maxIterations = 100;
% Initial guess
x0 = [0; 0];
% Initialize variables
x_k = x0;
for k = 1:maxIterations
% Calculate the values of the functions and Jacobian at x_k
f_k = [f1(x_k(1), x_k(2)); f2(x_k(1), x_k(2))];
J_k = Jacobian(x_k(1), x_k(2));
% Newton's method update
delta_x = -J_k\f_k;
x_k = x_k + delta_x;
% Check for convergence
if norm(delta_x) < tolerance
break;
end
end
% Display the solution
fprintf('Approximate Solution: x = %.6f, y = %.6f\n', x_k(1), x_k(2));
Part C
Use the appropiate MATLAB function to solve the same problem and verify you solutions in Part B.
% Your code goes here:
% Define the system of equations as a function handle
fun = @(x) [f1(x(1), x(2)); f2(x(1), x(2))];
% Initial guess
x0 = [0; 0];
% Solve the system of equations using fsolve
options = optimoptions('fsolve', 'Display', 'off');
[x_fsolve, ~, exitflag] = fsolve(fun, x0, options);
if exitflag > 0
fprintf('Solution found using fsolve: x = %.6f, y = %.6f\n', x_fsolve(1), x_fsolve(2));
else
fprintf('fsolve did not converge to a solution.\n');
end
% Compare with the solution from Part B
fprintf('Solution from Part B: x = %.6f, y = %.6f\n', x_k(1), x_k(2));
end
  6 commentaires
Walter Roberson
Walter Roberson le 11 Oct 2023
"The code will not run" is not sufficiently precise. You should be more specific about what you observe.
Note by the way that you never call your bisection function -- and that you are not asked to write any functions.
James
James le 11 Oct 2023
I mean that it did not show the graph to these problems. It did not show the answers to the code.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 12 Oct 2023
You need to split the code into different files.
Problem 1:
Note that you never call the function Bisection.
Part A
% Your code goes here:
% Define the function
f = @(x)x.*log(x) - 3;
% Plot the function
x = linspace(0.5, 10, 100);
y = f(x);
plot(x, y);
grid on;
Part B
% Your code goes here:
% Bisection method function
a = 0.5;
b = 1;
for n=1:20
c=(a+b)/2; % Bisection
if abs(f(c))<1.e-10
a=c;b=c; break
elseif f(a)*f(c)<0
b=c;
else
a=c;
end
end
c=(a+b)/2
c = 1.0000
c = 1.0000
c = 1
function x = Bisection(f, a, b, ~)
error = 1e-4;
% Check if the interval contains a root
if f(a)*f(b) > 0
error('The interval does not contain a root.');
end
% Initialize the midpoint
x = (a + b)/2;
% Iterate until the error is within tolerance
while abs(f(x)) > error
if f(x)*f(a) < 0
b = x;
else
a = x;
end
x = (a + b)/2;
end
% Display the solution
fprintf('The solution to the equation is x = %f.\n', x);
end
  2 commentaires
Walter Roberson
Walter Roberson le 12 Oct 2023
Problem 2:
Find the solutions to the equation:
sin(x)+1x+1=5
Part A.
Plot the appropriate function and find initial estimates, or intervals, for the location of the solutions.
% Your code goes here:
% Define the function
f = @(x) sin(x) + 1/(x + 1) - 5;
fplot(f,[-2,2]);grid on
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
% Bisection
a=0;b=5; %c=0.2608
a=0;b=5; %c=0.9349
a=1.2;b=2;
for n=1:30
c=(a+b)/2; % Bisection
if abs(f(c))<1.e-10
a=c;b=c; break
elseif f(a)*f(c)<0
b=c;
else
a=c;
end
end
c=(a+b)/2
c = 2.0000
try
xsoln=fzero(f,[0,2])
catch ME
disp(ME)
end
MException with properties: identifier: 'MATLAB:fzero:ValuesAtEndPtsSameSign' message: 'Function values at the interval endpoints must differ in sign.' cause: {} stack: [4×1 struct] Correction: []
try
xsoln=fzero(f,[0,5])
catch ME
disp(ME)
end
MException with properties: identifier: 'MATLAB:fzero:ValuesAtEndPtsSameSign' message: 'Function values at the interval endpoints must differ in sign.' cause: {} stack: [4×1 struct] Correction: []
try
xsoln=fzero(f,[0,2])
catch ME
disp(ME)
end
MException with properties: identifier: 'MATLAB:fzero:ValuesAtEndPtsSameSign' message: 'Function values at the interval endpoints must differ in sign.' cause: {} stack: [4×1 struct] Correction: []
hold on;plot(c,f(c),'*r');hold off
Part B
Write a script that uses the False Position method to find the solutions to the equation.
% Your code goes here:
% Your code goes here
clear
f = @(x) sin(x) + 1/(x + 1) - 5;
fplot(f,[1.2,2]);grid on
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
% Bisection
a=0;b=0.5; %c=0.2608
a=0.5;b=1.2; %c=0.9349
a=1.2;b=2;
for n=1:80
%c=(a+b)/2; % Bisection
m=(f(b)-f(a))/(b-a); % False Position
c=a-f(a)/m;
if abs(f(a)*f(c))<1.e-10
a=c;b=c; break
elseif f(a).*f(c)<0
b=c;
else
a=c;
end
end
c=(a+b)/2
c = 2.8512e+47
disp([a,b])
1.0e+47 * 5.7024 0.0000
try
xsoln=fzero(f,[0,0.5])
catch ME
disp(ME)
end
MException with properties: identifier: 'MATLAB:fzero:ValuesAtEndPtsSameSign' message: 'Function values at the interval endpoints must differ in sign.' cause: {} stack: [4×1 struct] Correction: []
try
xsoln=fzero(f,[0.5,1.2])
catch ME
disp(ME)
end
MException with properties: identifier: 'MATLAB:fzero:ValuesAtEndPtsSameSign' message: 'Function values at the interval endpoints must differ in sign.' cause: {} stack: [4×1 struct] Correction: []
try
xsoln=fzero(f,[1.2,2])
catch ME
disp(ME)
end
MException with properties: identifier: 'MATLAB:fzero:ValuesAtEndPtsSameSign' message: 'Function values at the interval endpoints must differ in sign.' cause: {} stack: [4×1 struct] Correction: []
hold on;plot(c,f(c),'*r');hold off
Walter Roberson
Walter Roberson le 12 Oct 2023
Problem 3:
(the first time)
Find the solutions to the equation:
x(log(x)+x)=5
Part A.
Plot the appropriate function and find initial estimates, or intervals, for the location of the solution.
% Your code goes here:
clc;clear all;close all;
%Part A
f=@(x)x.*(log(x)+x)-5;
df=@(x)log(x)+2+2*x;
x=1:0.01:3;
plot(x,f(x))
Part B
Write a script that uses the Newton's method to find the solution to the equation.
% Your code goes here:
%Part B
f=@(x)x.*(log(x)+x)-5;
df=@(x)log(x)+2+2*x;
es=0.001;
x=2;
x=Newton(f,df,x,es)
x = 1.9311
Part C
Use the appropiate MATLAB function to solve the same problem and verify you solutions in Part B.
% Your code goes here:
%part C
try
X=fzero(f,2)
catch ME
disp(ME)
end
X = 1.9311
%function definitions must appear at the end
function x=Newton(f,df,x0,es)
while (1)
x=x0-f(x0)/df(x0);
if abs((x-x0)/x)*100<es
break
end
x0=x;
end
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Chemistry dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by