Use of Matlab function in Simulink

I am trying to write a simulink model for the linear portion of a piston/rotating clutch system. It's a pretty simply simulink model but I am having trouble implementing a function that accounts for the angle between the piston and the clutch. I have written and tested the function in Matlab for validity. The modified code that was put into the Matlab Function box is as follows:
function theta = fcn(r1)
r1=[6.8:.01:8.33]
r2=2.5;
xc=8.09929498;
yc=-2.17646643;
a=(-4*yc^2-4*xc^2);
b=(-4*xc*r2^2+4*xc^3+4*xc*yc^2+4*xc*r1.^2);
c=(-r2^4+2*r2^2*xc^2+2*r2^2*yc^2+2*r2^2*r1.^2-xc^4-2*xc^2*yc^2-2*xc^2*r1.^2-yc^4+2*yc^2*r1.^2-r1.^4);
x=(-b-sqrt(b.^2-4*a.*c))/(2*a);
y=sqrt(r1.^2-x.^2);
theta1=atan(x./y);
theta2=atan((xc-x)./(yc-y));
theta=theta1-theta2;
It takes in r1 from 2 subsequent integrations and theoretically outputs an angle. This angle is then input into one of the forces that drives the accelleration of r1 (in a simple f=ma type model).
I am getting myriad errors. Any ideas why? Any help would be wonderful as I am rather new to simulink and especially this function box.

2 Comments

Hi Gregory,
Could you paste the errors here ?
Just curious, why would you want to initialize the variable 'r1' inside your function, when you are taking 'r1' as an input ?
Hi Gregory,
Could you paste the errors that you are receiving ?
Just curious, why would you want to initialize the variable 'r1' when you are getting that value as in input to your function ?

Sign in to comment.

Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Products

Asked:

on 24 Feb 2015

Commented:

on 24 Feb 2015

Community Treasure Hunt

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

Start Hunting!