The code will not run when I tried to write the code successfully, so try fixing the code, please.
1 次查看(过去 30 天)
显示 更早的评论
Problem 1:
Find the solutions to the equation:
x log(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
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+(2y−1)2=3
(x−1)2+(y−1)2−xy=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
(x−1)2+(y−1)2−sin(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 个评论
Walter Roberson
2023-10-11
"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.
回答(1 个)
Walter Roberson
2023-10-12
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
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 个评论
Walter Roberson
2023-10-12
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
try
xsoln=fzero(f,[0,2])
catch ME
disp(ME)
end
try
xsoln=fzero(f,[0,5])
catch ME
disp(ME)
end
try
xsoln=fzero(f,[0,2])
catch ME
disp(ME)
end
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])
try
xsoln=fzero(f,[0,0.5])
catch ME
disp(ME)
end
try
xsoln=fzero(f,[0.5,1.2])
catch ME
disp(ME)
end
try
xsoln=fzero(f,[1.2,2])
catch ME
disp(ME)
end
hold on;plot(c,f(c),'*r');hold off
Walter Roberson
2023-10-12
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)
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
%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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!