Using a matrix as an input argument for a function
22 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm currently trying to solve a problem in Matlab that involves using two input arguments in a function, one of which is a matrix. My problem is that within the Matrix are the variables y, z, Vy and Vz which Matlab recognises as undefined.
The code I'm having trouble with is this:
[dotx]=me12ttdP4(t, x)
x=([y;z;Vy;Vz]);
Vdoty=(K2(z-y)+B(Vz-Vy))/M;
Vdotz=(K1(u-z)+K2(z-y)-B(Vz-Vy))/m;
Ydot=Vy;
Zdot=Vz;
dotx=[Ydot;Zdot;Vdoty;Vdotz];
I get the error in the command window when I input
>> me12ttdP4(3, [1;2;0;3])
The error is:
Undefined function or variable 'y'.
Error in me12ttdP4 (line 6)
x=([y;z;Vy;Vz]);
Thank you for your time
0 个评论
回答(4 个)
Image Analyst
2014-2-23
Why do you think that the me12ttdP4() function should know what y is when you didn't pass it in or define it inside me12ttdP4() itself? It won't know what it is until you give it a value.
0 个评论
Sagar Damle
2014-2-23
I think,this will give you correct answer.
[dotx]=me12ttdP4(t, x)
y = x(:,1); z =x(:,2); Vy =x(:,3); Vz =x(:,4);
Vdoty=(K2(z-y)+B(Vz-Vy))/M;
Vdotz=(K1(u-z)+K2(z-y)-B(Vz-Vy))/m;
Ydot=Vy;
Zdot=Vz;
dotx=[Ydot;Zdot;Vdoty;Vdotz];
But my question is - you have not defined values of K1,K2,M and m in function definition of 'mel2ttdP4()'.How can you get correct output without this? Oh,are these global variables or such a thing?
4 个评论
Sagar Damle
2014-2-23
Sorry for my mistake! I think you want to get product of two terms - K2 and (z-y).So you have to write this as below -
Vdoty=(K2*(z-y)+B*(Vz-Vy))/M;
Similarly, Vdotz=(K1*(u-z)+K2*(z-y)-B*(Vz-Vy))/m;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!