简体   繁体   中英

How to fix 'parameter' undefined in Octave

I'm trying to write a script that will plot a ratio that is calculated between two functions. After multiple attempts at rewriting the functions, I still get the same error that X is undefined. What is being declared inproperly?

Function Code (surface_area.m):

function surface_area(x)
  surface_area = 6 .* (x .^ 2);
end
function volume(x)
  volume = x.^3;
end

Main Script (main.m):

x = linspace(0.1,20);
surface_area;

sa = surface_area(x);
volume = volume(x);
r = sa ./ volume;

plot(x, r)

Error Message (running from the main.m script):

error: 'x' undefined near line 2 column 24
error: called from
    surface_area at line 2 column 16
    main at line 2 column 1

Thank you for your assistance!

When you write surface_area , the surface_area function is called with 0 parameters. Within the function, x is undefined.

Additionally, volume is not visible nor accessible from outside the surface_area.m file. You can either put it into its own file (called volume.m ), or you can put it into your script file. But you need to make sure your script file does not start with the definition of a function, because that turns it into a function file. See here for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM