how to write a code for two input -one output function (f=@(v,r)) using bisection method?

%declaration of constants and variables
pi=3.14;
G=6.67*10^(-11);M=2*10^30;c=10^4;rho=10^(-21);n=2.5;
gamma=1+(1/n);
dotm=pi*G^2*M^2*(rho/c^3)*(2/(5-3*gamma))^((5-3*gamma)/(2*gamma-2));
u=(dotm/(4*pi*rho))*c^(2*n);
%span of v
v1=10;
v2=10^5;
tol=10^-10;
v=v1:c:v2;
%span of r
r1=.1*7*10^8;
r2=3.8*7*10^8;
r=r1:7*10^8:r2;
%function declaration
f=@(v,r)((v.^2)./2)+(n.*((u./(v.*r.^2)).^(1./n)))-((G*M)./r)-(n.*c.^2);

6 Comments

A bi-section method is meaningful in one dimension only.
What is the prupose of defining v and r as vectors, but as inputs of f also?
Just a note: While 6.67*10^(-11) is an expensive power operation, 6.67e-11 is a cheap constant and easier to read.
The input vectors are the value of r and v.for each value of r we have to find the corresponding two values of v by bisection method.then have to run the loop for each value of r.how to solve?
And why do you give values to v if the vector is unknown ?
Yes, that's a good point.undersuch situation how to solve?
If f(v1,r) and f(v2,r) should be of opposite sign according to initial condition of bisection method
Before we can suggest some code, you have to mention explicitly, what you want to calculate. I guess, you wanted to use the bisection method to find a root of the function? Then an optimization tool should be applicable for the 2-dim case.

Sign in to comment.

Answers (1)

pi = 3.14; % Brrrr
G = 6.67e-11; M = 2e30; c = 1e4; rho = 1e-21; n = 2.5;
gamma = 1 + 1/n;
dotm = pi*G^2*M^2*(rho/c^3) * (2/(5-3*gamma))^((5-3*gamma)/(2*gamma-2));
u = dotm / (4*pi*rho) * c^(2*n);
v1 = 10;
v2 = 1e5;
r1 = 0.7e8; % .1*7*10^8;
r2 = 3.8*7e8;
f = @(y) y(1)^2 / 2 + (n * ((u / (y(1)*y(2)^2)).^(1/n))) - (G*M)/y(2) - n*c^2;
x = fmincon(f, [v1+v2, r1+r2]/2, [], [], [], [], [v1, r1], [v2, r2])
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2
1.0e+07 * 0.0100 7.0000

Products

Release

R2022a

Asked:

on 30 Dec 2022

Edited:

Jan
on 30 Dec 2022

Community Treasure Hunt

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

Start Hunting!