Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Does anyone know how to do any of these programs?

Subject: Does anyone know how to do any of these programs?

From: a999

Date: 29 Nov, 2008 03:02:28

Message: 1 of 7

Does anyone know how to do any of these programs? help will be much appreciated.

4.3.1 Convergence of the bisection method
Implement the bisection method in a computer code, and compute the roots of the quadratic equation
x2 − 2 x + 0.9 = 0. Prepare and discuss a graph of the error against the iteration count, k.

4.5.2 Newton’s method
(a) Compute all zeros of the function f(x) = ln |x|+3−3.1 x2, accurate to the eighth decimal place.
Explain your choice of initial guess.

4.5.3 More on Newton’s method
(a) The function f(x) = x ln x has a root at x = 0. What is the rate of convergence of Newton’s
method toward this root?

4.5.4 Redlich-Kwong equation of state
Write a program that produces and prints a table showing the molecular volume of hydrogen for
fifteen combinations corresponding to pressure p = 1, 2, 3, 4, and 5 atm and temperature T = 200,
300, and 400 ◦K, based on the Redlich-Kwong equation of state (4.1.10). For the initial guess, use the
predictions of the ideal gas law. Discuss the physical significance of your results. Perry’s Chemical
Engineer’s Handbook (McGraw-Hill, fifth edition, pp. 3–41, 3–104) gives the following information
for hydrogen: Chemical formula: H2; Boiling Point at 1 atm: −252.7◦C; critical conditions: Tc =
−239.9◦C; Pc = 12.8 atm.

4.5.5 Viscous flow in a corner
The nonlinear equation
sin[2(x − 1) ] = (1 − x) sin(2 ), (1)
describes viscous flow in a corner bounded by two intersecting walls with aperture angle 2 ; the
variable x is a measure of the strength of the flow. A trivial solution for any is x = 1. Find and
plot another solution branch, X( ), in the range 0 < < .

4.6.4 A system of two equations
Compute one solution of the system
(x − 2)2 + (y − 3)3 + (x − 2.1)(y − 3.1) = 2.81, 10 e−x + 5 e1−y = 0.7468, (1)
using (a) Newton’s method, and (b) Newton’s method with the Jacobian evaluated only at the
beginning and then held constant. Compare the respective rates of convergence.

Subject: Does anyone know how to do any of these programs?

From: Roger Stafford

Date: 29 Nov, 2008 03:46:36

Message: 2 of 7

a999 <alikohbodi@yahoo.com> wrote in message <8659463.1227927779634.JavaMail.jakarta@nitrogen.mathforum.org>...
> Does anyone know how to do any of these programs? help will be much appreciated.
> ..........

  If you have any particular feature of one of these problems that involves some Matlab coding techniques, you might be able to persuade someone in this group to help you with it, but surely not unless you have already made significant progress on it yourself. We are not in the business of doing general homework assignments, but only that of answering certain specific questions concerned with the use of Matlab.

Roger Stafford

Subject: Does anyone know how to do any of these programs?

From: Husam Aldahiyat

Date: 29 Nov, 2008 05:44:02

Message: 3 of 7

Those questions seem fun and informative. I'm sure when you solve and learn the intricate details of your methods you will grow satisfied at the end.

Subject: Does anyone know how to do any of these programs?

From: John

Date: 29 Nov, 2008 19:56:01

Message: 4 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ggqdus$bu7$1@fred.mathworks.com>...
> a999 <alikohbodi@yahoo.com> wrote in message <8659463.1227927779634.JavaMail.jakarta@nitrogen.mathforum.org>...
> > Does anyone know how to do any of these programs? help will be much appreciated.
> > ..........
>
> If you have any particular feature of one of these problems that involves some Matlab coding techniques, you might be able to persuade someone in this group to help you with it, but surely not unless you have already made significant progress on it yourself. We are not in the business of doing general homework assignments, but only that of answering certain specific questions concerned with the use of Matlab.
>
> Roger Stafford

Ok, thank you for informing me, that is no problem. I am new to matlab and a lot of the simple things that i want to do get me stuck, and i get frustrated since then it doesnt even matter if i know what to do, I cant do it cause i am not familiar with the process and the language.
For the first problem: Implement the bisection method in a computer code, and compute the roots of the quadratic equation
x2 − 2 x + 0.9 = 0. Prepare and discuss a graph of the error against the iteration count, k.

I have written the following the code:
clc; clear all; close all;
y(x)=(x^2)-(2*x)+(0.9);
X1=2;
X2=-2;
Xm=(0.5)*(y(X1)+y(X2));
d=X2-X1;
Xnewm=Xm+(0.5*d);
disp(Xnewm);

result:
??? Undefined function or variable 'x'.

Error in ==> hw5p1 at 2
y(x)=(x^2)-(2*x)+(0.9);

this is probably a stupid question but how do i plug in X1 and X2 into the y(x) equation, what format do i have to use for that?
thanks you

Subject: Does anyone know how to do any of these programs?

From: Roger Stafford

Date: 30 Nov, 2008 01:07:01

Message: 5 of 7

"john " <john@yahoo.com> wrote in message <ggs6oh$hrq$1@fred.mathworks.com>...
> .........
> Ok, thank you for informing me, that is no problem. I am new to matlab and a lot of the simple things that i want to do get me stuck, and i get frustrated since then it doesnt even matter if i know what to do, I cant do it cause i am not familiar with the process and the language.
> For the first problem: Implement the bisection method in a computer code, and compute the roots of the quadratic equation
> x2 − 2 x + 0.9 = 0. Prepare and discuss a graph of the error against the iteration count, k.
>
> I have written the following the code:
> clc; clear all; close all;
> y(x)=(x^2)-(2*x)+(0.9);
> X1=2;
> X2=-2;
> Xm=(0.5)*(y(X1)+y(X2));
> d=X2-X1;
> Xnewm=Xm+(0.5*d);
> disp(Xnewm);
>
> result:
> ??? Undefined function or variable 'x'.
>
> Error in ==> hw5p1 at 2
> y(x)=(x^2)-(2*x)+(0.9);
>
> this is probably a stupid question but how do i plug in X1 and X2 into the y(x) equation, what format do i have to use for that?
> thanks you
-----------
  Since you have attempted some work on it, I will help you to the following extent on problem 4.3.1.

  In the bisection method you want to make sure you start out with two values of x that produce opposite signs for the corresponding values of y. Your x1 = 2 and x2 = -2 constitute an unfortunate choice for that since they both give positive values for y. Make a better selection. In fact notice that there are two solutions to the equation, so some pairs of x1, x2 should surround one of these and other pairs should be grouped around the other root.

  You first want to start with a pair of x1 and x2 for which you have computed the corresponding values of y as y1 and y2 and these must be of opposite sign. Then enter into a 'while' loop inside of which you would do something like the following:

 xm = (x1+x2)/2; % Find the midpoint between x1 and x2
 ym = xm^2-2*xm+.9; % Compute the corresponding y value
 if sign(ym) ~= sign(y1) % Ask if ym and y1 are of opposite signs
  x2 = xm; % If so, alter x2 and y2 to the new values, xm and ym
  y2 = ym;
 else % Otherwise ym and y2 must be of opposite signs
  x1 = xm; % So alter x1 and y1 to the new values
  y1 = ym;
 end
% No matter which alternative was selected, the new y1 and y2
% will still be of opposite sign
 err = abs(y2-y1); % Check the new difference in y values

At this point you now have the desired root pinned between the new values of x1 and x2 which are half as far apart as in the last trip through the 'while' loop. Make the 'while' loop quit whenever the error (err) becomes acceptably small.

  You will have to augment this stuff by gathering the iteration count and error in some array for later graphing to satisfy the requirements of the problem.

  If this conditional looping process still puzzles you, make a point of carefully studying how the 'while' command works. It is absolutely fundamental to the operation of many, many Matlab programs.

Roger Stafford

Subject: Does anyone know how to do any of these programs?

From: John

Date: 30 Nov, 2008 02:24:01

Message: 6 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ggsovl$svb$1@fred.mathworks.com>...
> "john " <john@yahoo.com> wrote in message <ggs6oh$hrq$1@fred.mathworks.com>...
> > .........
> > Ok, thank you for informing me, that is no problem. I am new to matlab and a lot of the simple things that i want to do get me stuck, and i get frustrated since then it doesnt even matter if i know what to do, I cant do it cause i am not familiar with the process and the language.
> > For the first problem: Implement the bisection method in a computer code, and compute the roots of the quadratic equation
> > x2 − 2 x + 0.9 = 0. Prepare and discuss a graph of the error against the iteration count, k.
> >
> > I have written the following the code:
> > clc; clear all; close all;
> > y(x)=(x^2)-(2*x)+(0.9);
> > X1=2;
> > X2=-2;
> > Xm=(0.5)*(y(X1)+y(X2));
> > d=X2-X1;
> > Xnewm=Xm+(0.5*d);
> > disp(Xnewm);
> >
> > result:
> > ??? Undefined function or variable 'x'.
> >
> > Error in ==> hw5p1 at 2
> > y(x)=(x^2)-(2*x)+(0.9);
> >
> > this is probably a stupid question but how do i plug in X1 and X2 into the y(x) equation, what format do i have to use for that?
> > thanks you
> -----------
> Since you have attempted some work on it, I will help you to the following extent on problem 4.3.1.
>
> In the bisection method you want to make sure you start out with two values of x that produce opposite signs for the corresponding values of y. Your x1 = 2 and x2 = -2 constitute an unfortunate choice for that since they both give positive values for y. Make a better selection. In fact notice that there are two solutions to the equation, so some pairs of x1, x2 should surround one of these and other pairs should be grouped around the other root.
>
> You first want to start with a pair of x1 and x2 for which you have computed the corresponding values of y as y1 and y2 and these must be of opposite sign. Then enter into a 'while' loop inside of which you would do something like the following:
>
> xm = (x1+x2)/2; % Find the midpoint between x1 and x2
> ym = xm^2-2*xm+.9; % Compute the corresponding y value
> if sign(ym) ~= sign(y1) % Ask if ym and y1 are of opposite signs
> x2 = xm; % If so, alter x2 and y2 to the new values, xm and ym
> y2 = ym;
> else % Otherwise ym and y2 must be of opposite signs
> x1 = xm; % So alter x1 and y1 to the new values
> y1 = ym;
> end
> % No matter which alternative was selected, the new y1 and y2
> % will still be of opposite sign
> err = abs(y2-y1); % Check the new difference in y values
>
> At this point you now have the desired root pinned between the new values of x1 and x2 which are half as far apart as in the last trip through the 'while' loop. Make the 'while' loop quit whenever the error (err) becomes acceptably small.
>
> You will have to augment this stuff by gathering the iteration count and error in some array for later graphing to satisfy the requirements of the problem.
>
> If this conditional looping process still puzzles you, make a point of carefully studying how the 'while' command works. It is absolutely fundamental to the operation of many, many Matlab programs.
>
> Roger Stafford
Thanks a lot for your response and help. I wrote the program before and then changed it based on your advice but somehow i can not get an answer from it.
Original version:
clc; clear all; close all;
y=inline('(x^2)-(2*x)+(0.9)');

X1=1;
X2=-2;
Xm=(0.5)*(X1+X2);
d=X2-X1;
Xnewm=X1+(0.5*d);

if y(X1)+y(Xm)<y(X1)
    X2=Xm;
else
    if y(X2)+y(Xm)<y(X2)
        X1=Xm;
    elseif y(Xm)==0
        disp(Xm);
    end
end
Changed version:
clc; clear all; close all;
y=inline('(x^2)-(2*x)+(0.9)');

X1=1;
X2=-2;
Xm=(0.5)*(X1+X2);
d=X2-X1;
Xnewm=X1+(0.5*d);
if sign(y(X1)) ~= sign((Xm))
    X2=Xm;
else
    if sign(y(X2)) ~= sign((Xm))
        X1=Xm;
    elseif y(Xm)==0
        disp(Xm);
    end
end

when i run it, it does not display Xm at the end.

Subject: Does anyone know how to do any of these programs?

From: Roger Stafford

Date: 30 Nov, 2008 03:44:02

Message: 7 of 7

"john " <john@yahoo.com> wrote in message <ggstg1$nae$1@fred.mathworks.com>...
> ........
> Changed version:
> clc; clear all; close all;
> y=inline('(x^2)-(2*x)+(0.9)');
>
> X1=1;
> X2=-2;
> Xm=(0.5)*(X1+X2);
> d=X2-X1;
> Xnewm=X1+(0.5*d);
> if sign(y(X1)) ~= sign((Xm))
> X2=Xm;
> else
> if sign(y(X2)) ~= sign((Xm))
> X1=Xm;
> elseif y(Xm)==0
> disp(Xm);
> end
> end
>
> when i run it, it does not display Xm at the end.
------------------
  I will limit my response to just a few observations, John. 1) You have not implemented the 'while' loop you need to do the necessary iterating. It only does its computation once. 2) You are comparing the sign of y(X1) with that of Xm which makes no sense. 3) You are waiting for y(Xm) to equal zero to display Xm which may never happen no matter how many iterations you have done. 4) You do a comparison with the sign of y(X2) after your comparison with the sign of y(X1). If you know that they remain always opposite in sign, this is totally unnecessary. 5) In bringing the 'while' loop to a halt you would need the absolute value of the difference in y's. This difference may never reach zero and you may not know in advance which sign it will have. 6) The quantity Xnewm is identically equal to Xm and is wholly redundant.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us