MATLAB Answers

How do I go about increasing the number of arguments in my code? Or how should I fix this issue?

2 views (last 30 days)
Mitch Foltarz
Mitch Foltarz on 25 Mar 2020
Answered: Geoff Hayes on 25 Mar 2020
clc,clear
a = 3;
b = 4;
% pi/10 = 18 degrees & 9pi/10 = 162 degrees
O_int = pi/10;
O_fin = (9*pi)/10;
O_inc = pi/1000;
n_elem = ((O_fin-O_int)/O_inc)+1;
% Terpintine & Night Witches
for n = 1:n_elem
O(n) = O_int+(n-1)*O_inc;
x_1(n) = a*cot(O(n));
y_1(n) = b*sin(O(n))*cos(O(n));
x_2(n) = a*cot(O(n));
y_2(n) = a*(sin(O(n)))^2;
end
x = linspace(1:n_elem:30);
O(x) = O_int+(x-1)*O_inc;
X_1(x) = a*cot(O(x));
Y_1(x) = b*sin(O(x))*cos(O(x));
X_2(x) = a*cot(O(x));
Y_2(x) = a*(sin(O(x)))^2;
figure(1)
plot (x_1,y_1)
hold on
plot (x_2,y_2)
title ('Multiple Plots')
xlabel('Angle Measure')
ylabel('Amplitude')
legend('Serpintine', 'Witch of Agnesi')
figure(2)
plot (X_1,Y_1)
hold on
plot (X_2,Y_2)
title ('Multiple Plots')
xlabel('Angle Measure')
ylabel('Amplitude')
legend('Serpintine', 'Witch of Agnesi')

Answers (1)

Geoff Hayes
Geoff Hayes on 25 Mar 2020
Mitch - there are a couple of errors around this block of code
x = linspace(1:n_elem:30);
O(x) = O_int+(x-1)*O_inc;
X_1(x) = a*cot(O(x));
Y_1(x) = b*sin(O(x))*cos(O(x));
X_2(x) = a*cot(O(x));
Y_2(x) = a*(sin(O(x)))^2;
The correct use of linspace would be to
x = linspace(1, 30, n_elem);
if you wish to create an array of n_elem elements in the interval [1,30]. This should fix the error
Error using linspace (line 19)
Not enough input arguments.
The second problem is with the next set of lines where you use x to index (?) into a handful of arrays. What is the intent here? Do you mean to do something more like
O = zeros(length(x),1);
X_1 = zeros(length(x),1);
X_2 = zeros(length(x),1);
Y_1 = zeros(length(x),1);
Y_2 = zeros(length(x),1);
for k = 1:length(x)
O(k) = O_int+x(k)*O_inc;
X_1(k) = a*cot(O(k));
Y_1(k) = b*sin(O(k))*cos(O(k));
X_2(k) = a*cot(O(k));
Y_2(k) = a*(sin(O(k)))^2;
end
I'm guessing though so perhaps you mean to do something else. Since these five arrays are of the same dimension, I might just create one array of dimension num_elemX5 rather than having five arrays.

  0 Comments

Sign in to comment.

Tags