4purs
4purs

Reputation: 13

Finding a root in matlab, not compiling

i have to do an assignment where i have to find the depth of how much the ball is under water depending on the water's density, then i have to plot it. i don't have matlab at home but i'm using octave and for some reason it's not compiling, and it's not saying any errors. here is my code

function my_script()

% weight function
function func = weight(x,p)
func = p *pi* 4- 3 * pi*x ^2 + pi* x ^3;


% make the secant method
function root = secant(func , p,  x0, x1, tol, count)

while count ~= 0

y1 = func(x0,p);
y2 = func(x1, p);
k = (x1 - x0)/(y2 - y1);
R = y2 * k;
root = x1 - R;

if abs(func(root,p)) < tol
break;
end

x0 = x1;
x1 = root;
count = count - 1;

end

%create array for depth values

y_val = [];

%input the roots into the array with a loop

for p = 0:0.01:1

t = secant(@weight, p, 0, 1, 0.000001, 1000000);
disp (t);
y_val = [y_val t];

end

% create the x - axis
x_val = 0 : 0.01 : 1;

%plotting the graph
figure;
plot (x_val, y_val)
xlabel('Density');
ylabel('Depth ');
title('Floating Sphere vs Density ');

Upvotes: 0

Views: 214

Answers (2)

Fausto Arinos Barbuto
Fausto Arinos Barbuto

Reputation: 398

Here's your code, Octave style. Observe that I temporarily commented the "disp(t);" statement to avoid a long listing of values. This way, the plot is plotted immediately. Hopefully this helps.

function my_script

  %create array for depth values

  y_val = [];

  %input the roots into the array with a loop

  for p = 0:0.01:1
    t = secant(@weight, p, 0, 1, 0.000001, 1000000);
    %disp (t);
    y_val = [y_val t];
  endfor

  % create the x - axis
  x_val = 0 : 0.01 : 1;

  %plotting the graph
  figure(1)
    plot (x_val, y_val)
    xlabel('Density');
    ylabel('Depth ');
    title('Floating Sphere vs Density ');

endfunction  

% make the secant method
function root = secant(func , p,  x0, x1, tol, count)

  while count ~= 0

    y1 = func(x0,p);
    y2 = func(x1, p);
    k = (x1 - x0)/(y2 - y1);
    R = y2 * k;
    root = x1 - R;

    if abs(func(root,p)) < tol
      break;
    end

    x0 = x1;
    x1 = root;
    count = count - 1;

  endwhile
endfunction

% weight function
function func = weight(x,p)
  func = p *pi* 4- 3 * pi*x ^2 + pi* x ^3;
endfunction

Upvotes: 2

David
David

Reputation: 8459

Another way of creating what you have written is to split it into three different functions.

You'd have the weight function, weight.m,

% weight function
function func = weight(x,p)
func = p *pi* 4- 3 * pi*x ^2 + pi* x ^3;

and the secant method function, secant.m,

% make the secant method
function root = secant(func , p,  x0, x1, tol, count)

while count ~= 0

y1 = func(x0,p);
y2 = func(x1, p);
k = (x1 - x0)/(y2 - y1);
R = y2 * k;
root = x1 - R;

if abs(func(root,p)) < tol
break;
end

x0 = x1;
x1 = root;
count = count - 1;

end

%create array for depth values

y_val = [];

%input the roots into the array with a loop

for p = 0:0.01:1

t = secant(@weight, p, 0, 1, 0.000001, 1000000);
disp (t);
y_val = [y_val t];

end

% create the x - axis
x_val = 0 : 0.01 : 1;

%plotting the graph
figure;
plot (x_val, y_val)
xlabel('Density');
ylabel('Depth ');
title('Floating Sphere vs Density ');

Then you'd have my_script():

function my_script()

but it is an empty function, so the output is correct! You have to have all the commands for each function finished before you define the next function.

I have a feeling that you want to take out everything from the end of the while loop onwards from the secant function and put it into the main my_script() function.

Upvotes: 2

Related Questions