Quadratic function not plotting

I need to calculate a basic quadratic function in Octave, and it just not plots. I got the correct answers, but it’s saying 0 is not an integer number, and that makes no sense. Here are a print of the code and onde of the error. Any help wold be greatful!

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

your notation for getting f(x) is not correct. the way you wrote it, you are saying the ‘x_th element of variable f’ is equal to your calculation in line 4. instead simply say:

f = x.^2

also, you then call plot with a variable d that you seem to have never defined. unless you’ve defined d elsewhere, that may create another error.

In addition, plot (x,f) (without d) will only work if you actually call your function that returns x and f.

Oh, thanks! And d was suposed to be ‘d’ (diamond), I just miswrote it, thanks for noticing it!

I have this little code right here and it’s just not plotting. It also doesn’t presents any errors, and I’ve done just as told in the GNU instructions. Any help would be greatful!

The end can be put at the end of the file to get a quick success.

However, the whole approach is flawed. Please try to carefully read Defining Functions (GNU Octave (version 6.3.0)) and decide if you want to define a function or a script. The given approach is a not working mix of both :sweat:

What are “GNU instructions”? Did you get this bad example from there? Maybe post the link if possible :wink:

The objective is to define a function. Although I got the answer I expected, I’d like to verify how it would be plotted. Maybe this is why it’s not working… Is it?

Good night. My code has a syntax error, but it doesn’t say what it is. Here’s the code and the error. The objective is to sum the length of the lines of a polygonal graph. I’ll keep studying, but it would be nice if someone show me what’s wrong.Poligonal.m (255 Bytes)

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

You have an extra bracket at the end of the line

Nice, it’s a progress! I removed it and added “.” before ^, but now the function doesn’t give me what it’s supposed to… Please, take a look

@Animus Please stop creating new topics for questions about the same topic and answer in this topic.

Can you please read the aforementioned section of the manual Defining Functions (GNU Octave (version 6.3.0)) and make the following changes to your program, otherwise you will make no progress:

  • A function is usually defined in a separate file with the same name as the function.
    • You mix up function declarations and functions calls. This is simply wrong for Octave.
    • Create a new file called f.m and put only the few lines of the function inside.
    • Create a new file called Poligonal.m and put the respective part of your code inside.
    • Create a new script file called myScript.m and put all the calling code inside.

Later you might learn how to combine things, but first it seems more helpful you get the idea of what is a function (input and output, which is wrongly used in the given example) and what is a script calling a function. Then you will easily understand what those error messages mean.

1 Like

Thanks for the advice. I’m sorry I flooded with the same topic, I’m honestly still learning.

Nice, done as you said! There’s just a last problem: It’s giving me the Poligonal(N) as equals to 11, and I suppose it’s because the part it calculates (f(x)-f(x-1)).^2 the program thinks equals 0. I’ll keep studying by myself, but if someone can figure out what’s happening it woult help a lot! Please, take a look: image

Don’t assign the output value to a local variable with the same name as the function. I think you want Y = sum (...); on that line, but I don’t know what you expect with that line inside the FOR loop.

Sorry, but I didn’t get it… About what line are you talking about?

@Animus in your screenshot @jwe talks about line 5.

It is difficult to guess what computations the functions “Poligonal” and “f” should perform. Let us focus on “f” first. What is “f” supposed to calculate? Is it f: \mathbb{R} \to \mathbb{R} with f(x) = x^2?

This function file f.m would look in Octave like

function y = f(x)
  y = x.^2;

and can be called with scalar input

>> y = f(2)
y = 4

and vector input

>> y = f([1, 2, 3, 4])
y =

    1    4    9   16

In the Poligonal program this distinction between scalar and vector quantities seems to be misunderstood. A rule of thumb for Octave: when you work with vectors, you do not use for-loops.

Ok, got it. Now the error is:
“error: operator +: nonconformant arguments (op1 is 1x9, op2 is 1x11)
error: called from
Poligonal at line 6 column 13”
I know what it means, but what am’I supposed to do? Isn’t this wxactly what I want?

since we can’t see the current state of your code, it’s hard to tell. do you still have a for loop? if so do you realize yet that your for loop only loop over one value of N (=10)?

I’m assuming you don’t actually want to add 1x9 and 1x11 variables. that would be like trying to add:

[1 2 3] + [1 2 3 4 5]     

the error message is trying to tell you that math can’t do that.

so which variable is 1x9, which is 1x11, and what do you actually want them to be in your calculation?

do you know how to step through your code in the Editor and check your variables along the way?

I’d like to say to y’all that thanks to you and my teacher the program is now complete and running perfectly!

1 Like

Ok, guys, one last thing: I need to pick up 2 points in each curve and estimate roughly an exponent that applies to them. The problem is every value I get is said to be the wrong one. My teacher just showd me how to do, but it’s still so confusing! This is the very last help I need with this program, I’ll let the whole program here. Thanks for everything until now!
Delta.m (82 Bytes)
Epsilon.m (109 Bytes)
Exercício 2.m (245 Bytes)
f.m (46 Bytes)
Poligonal.m (167 Bytes)