How can I send my radian values to a function?

Hello, Im trying to send my q values to a function. q values is changing from 0 to 2pi. The function is for taking derivative of ra for each q values. And then I have to plot a graph derivative values for each q values(y axis) - q values as degrees(x axis). I dont know what is problem is. Function code;
function [ dra ] = Derivative( x )
a=0.016489;
b=0.015;
c=0.027;
d=0.075;
h=0.010;
x1=(a*c*sin(x)-h*c+b*d+a*b*cos(x))/(d-c+a*cos(x));
theta=atan((x1-b)/c);
x3=sqrt((d.^2)+(a.^2)+(x1.^2)-2*x1*h+(h.^2)+2*(x1-h)*a*sin(x));
ra=a*exp(j*x)-x3*exp(j*theta);
dra=diff(ra,x)
end
Main program;
clear all
clc
m=0:(pi/18):(2*pi);
m=m.'
n=length(m);
ddata=zeros(n,1);
for i=1:n
q = (i-1)*(pi/18);
[f] = Derivative(q)
data(i)=f(q);
end
disp(data)
degrees=m*(180/pi);
plot(degrees,data)
xlabel('Angle');
ylabel('Velocity of Point A');
title('Velocity Graph')
I have these errors when I run the code.
Error using diff Difference order N must be a positive integer scalar.
Error in Derivative (line 12) dra=diff(ra,x)
Error in deneme2 (line 12) [f] = Derivative(q) Thanks,

3 commentaires

KSSV
KSSV le 27 Oct 2016
In function Derivative, what do you mean by last but one line (before end) dra=diff(ra,x)???? What exactly you are trying to do here?
starrob
starrob le 27 Oct 2016
Modifié(e) : starrob le 27 Oct 2016
Im trying to take derivative of 'ra' according to x. Then i am using derivative of ra on main code for each q values.
KSSV
KSSV le 27 Oct 2016
There is problem with that line......I suggest you to read documentation part for diff.

Connectez-vous pour commenter.

 Réponse acceptée

Use a version of the definition of the derivative to take the derivative:
df/dx = (f(x+h) - f(x)) / h
(Use 1E-8 for ‘h’.) The diff function takes the difference between elements of a vector. It is an ‘approximate derivative’, and since you are calculating the derivative at each value of ‘x’ rather than the derivative of a vector, you need to code your function differently.
With that change (and a few other tweaks, such as creating ‘ra’ as an anonymous function), this runs. (See the section on ‘Anonymous Functions’ in Function Basics for details on how to create and use them.) I will let you determine if it gives the result you want, and to understand how it works.
The (revised) Code:
function [ dra ] = Derivative( x )
a=0.016489;
b=0.015;
c=0.027;
d=0.075;
h=0.010;
x1=(a*c*sin(x)-h*c+b*d+a*b*cos(x))/(d-c+a*cos(x));
theta=atan((x1-b)/c);
x3=sqrt((d.^2)+(a.^2)+(x1.^2)-2*x1*h+(h.^2)+2*(x1-h)*a*sin(x));
ra = @(x) a*exp(j*x)-x3*exp(j*theta);
dra = (ra(x+1E-8) - ra(x)) / 1E-8; % Calculate Derivative
end
% Main program;
m=0:(pi/18):(2*pi);
m=m.';
n=length(m);
ddata=zeros(n,1);
for i=1:n
q = (i-1)*(pi/18);
data(i) = Derivative(q);
end
% disp(data)
degrees=m*(180/pi);
plot(degrees,real(data), degrees,imag(data))
xlabel('Angle');
ylabel('Velocity of Point A');
title('Velocity Graph')
legend('Real', 'Imag')

2 commentaires

starrob
starrob le 2 Nov 2016
Thank you very much, yes! now its work but i still have one problem. In derivative function, I want to assign a constant value to derivative of x, like dx/dt=x'=140 rpm. Is it possible to do this? any ideas?
If you want to assign a constant value to the derivative, just do it in the loop:
for i=1:n
q = (i-1)*(pi/18);
data(i) = 140;
end
or more simply still:
data = 140*ones(1,n);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by