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

1 Ansicht (letzte 30 Tage)
Gabriel Donato
Gabriel Donato am 18 Jul. 2021
Bearbeitet: Gabriel Donato am 19 Jul. 2021
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 f = exponecial(N)
for x = linspace(0, 1, N)
f = x^2;
endfor
end
figure (1);
hold;
for N = 0:1:10
plot(N, exponecial(N))
endfor
  3 Kommentare
VBBV
VBBV am 19 Jul. 2021
It seems you know microsoft Visual Basic programming well. But in matlab environemnt those syntax may not work correctly.
Gabriel Donato
Gabriel Donato am 19 Jul. 2021
Bearbeitet: Gabriel Donato am 19 Jul. 2021
Actually I`m using Octave cause I cant afford the original MATLAB software. The curious thing is that I just addapted the same structure from a previous code I`ve done before, a code that worked just as I wanted... As you said before, someone just solved the problem below. I aprecciate the help.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 18 Jul. 2021
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
fprintf('Beginning to run %s.m ...\n', mfilename);
legendStrings = cell(1, 10);
for N = 1:10
[x, f] = exponecial(N);
plot(x, f, '.-', 'LineWidth', 2, 'MarkerSize', 40)
hold on;
legendStrings{N} = sprintf('N = %d', N);
end
grid on;
xlabel('x', 'fontSize', fontSize);
ylabel('f', 'fontSize', fontSize);
legend(legendStrings, 'Location', 'northwest');
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m.\n', mfilename);
function [x, f] = exponecial(N)
x = linspace(0, 1, N);
f = x.^2;
end
  1 Kommentar
Gabriel Donato
Gabriel Donato am 19 Jul. 2021
I`m sorry to say that, but I`m using Octave (can`t afford MatLab) and it`s still not running...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by