How do I plot a graph linearly spaced by N elements?

I have to create a poligonal graph and calculate its lenght, and this is the first step: evenly spacing the graph of the f = x^2 function. I wrote the code and it gives me just a blank plot. Here’s the code, I don’t know what’s wrong with it. I’d ble glad if someone helped me.

clc; clear all; close all;

function [x, f] = exponencial(N)
  x = linspace(0, 1, N);
  f = x^2;

for N = 10
  [x, f] = exponencial(N);
  plot(x, exponencial(N), '.-');

My system

  • OS: e.g. Windows 10 (version 2004) or Ubuntu 20.04
  • Octave version: e.g. Version 6.1.0
  • Installation method: e.g. Downloaded and installed “octave-6.1.0-w64-installer.exe” from Download

Copying your code into a script file myscript.m gives the following result and no plot at all:

error: for x^y, only square matrix arguments are permitted and one argument must be scalar.  Use .^ for elementwise power.
error: called from
    exponencial at line 5 column 5
    myscript at line 9 column 10

To resolve this error, read Arithmetic Ops (GNU Octave (version 6.1.0)). In Octave, there is a distinction between .* and * (note the dot before the star operator). If you need more help on this topic, a web search will get you plenty of questions and answers to this topic.

In the second for loop, you loop only over a single value 10 probably you want to see more and some comments to the loop body:

[x, f] = exponencial(N);
plot(x, exponencial(N), '.-');  # effectively:  plot(x, x, '.-');
hold;  # hold on; ?

Octave help to hold.

I’ve tried what you sugested. Now it runs, but with no results…

Can you be more precise what “now it runs” and “no results” means?

As @siko1056 pointed out, your inner code is probably not what you want. I would try

[x, f] = exponencial(N);
plot(x, f, '.-');
hold on;

Doesn’t run…

Please describe, what you expect to happen / to be computed.

The screenshot shows that the plotting line is plot(x, x.^2) (instead of plot(x,f)) and variable x is likely not defined in that scope.

Thanks a lot! I worked!!!

I need to crate a program that calculates and somes the distances between n points in a polygonal graph. I’m new at programming, and I don’t have any idea of how to accomplish this thask. Here’s the code with the graph I need to use. I’d really appreciate any help!
teste.m (237 Bytes)