V simple graphing in Matlab Questions

Mar 2016
170
0
Australia
Hi,

As part of a bigger question I have to create a simple plot in Matlab.

Could anyone tell me where I am going wrong in the simple script below:

%Test Plot
x=-2:0.01:4;
D=sqrt(x.^2+(x.^2-2*x-4).^2);
Plot(x,D);

Kind regards
Beetle
 

romsek

MHF Helper
Nov 2013
6,828
3,073
California
seems to be plot not Plot, i.e. lower case "p"
 
Mar 2016
170
0
Australia
Hi,

I tried my little plot in the actual problem I am trying to do and am still getting the same error message.

The Error Message is:

Error in Assg2Q2Figure74v1 (line 9)
plot(xvec,Dvec)

My input is:
%Compute Distance from Origin of Parabola
clear
x=-2:0.01:4;
D=length(x);
disp('Distance from Origin is')
%Plot Distance against Time
xvec=-2:0.01:4;
Dvec=length(xvec);
plot(xvec,Dvec)
xlable('x Values')
ylable('Distance from Origin')
Title('Graph Showing Distance from Origin of Given Original Parabola')

and the function file (length) is:
function D=length(x)
%Compute distance from origin for given parent parabola
D=sqrt(x.^2+(x.^2-2*x-4).^2);

Is anyone able to help.

Kind regards
Beetle
 

romsek

MHF Helper
Nov 2013
6,828
3,073
California
length is a built in matlab function to return the number of elements in a vector.

I don't know if you can override it's use like you are doing.

Can you try giving that function a different name?
 
Last edited:
Mar 2016
170
0
Australia
Hi tried giving it another name and got a massive string of errors:

Input:
%Compute Distance from Origin of Parent Parabola
clear
x=-2:0.01:4;
Dist=dog(x);
disp('Distance From Origin is')
disp('Dist')
%Plot values between -1 and 4 Showing Distance
xvec=(-2:0.01:4);
Dvec=dog(xvec);
plot(xvec,Dvec)
xlabel('x Points')
ylabel('Dist From Origin')
title('Graph Showing Distance from Origin of Parent Function'

Function file dog.m
function D=dog(x)
%Compute distance from origin for given parent parabola
D=sqrt(x.^2+(x.^2-2*x-4).^2);

Errors:
>> untitled6
Distance From Origin is
Dist
Undefined operator '.^' for input arguments of type
'matlab.graphics.axis.Axes'.

Error in length (line 3)
D=sqrt(x.^2+(x.^2-2*x-4).^2);

Error in cla (line 19)
if nargin>0 && length(varargin{1})==1 &&
ishghandle(varargin{1}) &&
strcmpi((get(varargin{1},'Type')),'axes')

Error in newplot>ObserveAxesNextPlot (line 124)
cla(ax, 'reset',hsave);

Error in newplot (line 73)
ax = ObserveAxesNextPlot(ax, hsave);

Error in untitled6 (line 10)
plot(xvec,Dvec)
 

romsek

MHF Helper
Nov 2013
6,828
3,073
California
what are the periods that are scattered through the code?

such as

D=sqrt(x.^2+(x.^2-2*x-4).^2);
 
Mar 2016
170
0
Australia
Hi, I was using them as required when mixing scalors with vectors, however if you think they are unnecesary that might be the case. The function code ran when I changed the p in the plot command earlier so from that I assumed that the formula had been entered correctly.