I just need this one TINY code to execute :( Any idea what the problem with this simple equation is?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
I need to put this Either in another function or as a seperate function. Or the script. It's accepting L, M, and b values, but i am not getting an output value for y.
suggestions?
prompt1 = 'Enter value of L';
prompt2 = 'Enter value of M';
prompt3 = 'Enter value of b';
L = input(prompt1)
M = input(prompt2)
b = input(prompt3)
y = (12*9.81*L)/(4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2);
Accepted Answer
Ameer Hamza
on 8 Nov 2020
Edited: Ameer Hamza
on 15 Nov 2020
You need to print the output. For example using disp()
prompt1 = 'Enter value of L';
prompt2 = 'Enter value of M';
prompt3 = 'Enter value of b';
L = input(prompt1)
M = input(prompt2)
b = input(prompt3)
y = (12*9.81*L)/(4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2);
disp(y)
Or remove the semicolon from the last line
y = (12*9.81*L)/(4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2)
%^ removed semicolon from here
8 Comments
Also your use of .^ suggests that possibly L will be a non-scalar. If so then
y = (12*9.81*L)./(4*L^.2)+(b.^2)-(12*L.*M)+(12*M.^2);
I tried that and it worked!!
I have to incorporate it into the rest of my code, and i’ve tried to do it as follows but there seems to be a problem.
Any idea what i can do to correct it?
function xdot = pend_cl(t1,x) %for linear
function z = wsq
prompt1 = 'Enter value of L';
prompt2 = 'Enter value of M';
prompt3 = 'Enter value of b';
L = input(prompt1)
M = input(prompt2)
b = input(prompt3)
disp(z) = (12*9.81*L)/((4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2));
end
xdot = [x(2); wsq*x(1)];
end
function ydot = pend_cn(t2,y) %for non linear
function z = wsq
prompt1 = 'Enter value of L';
prompt2 = 'Enter value of M';
prompt3 = 'Enter value of b';
L = input(prompt1)
M = input(prompt2)
b = input(prompt3)
disp(z) = (12*9.81*L)/((4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2));
end
ydot = [y(2); wsq*sin(y(1))];
end
clear all;
clc;
clf;
tic;
tspan = 0:0.01:4.5;
a=pi/2;
b=0;
x0 = [a; b]; %a is initial disp,
%b is initial velocity
[t1,x] = ode45(@pend_cl,tspan,x0);
X1 = x(:,1); %X1 is angular disp for linear
X2 = x(:,2); %X2 is angular accln for linear
y0 = [a ; b];
[t2,y] = ode45(@pend_cn,tspan,y0);
Y1 = y(:,1); %Y1 is angular disp for non linear
Y2 = y(:,2); %Y2 is angular accln for non linear
figure(1);
subplot(2,2,1);
plot(t1,X1);
xlabel('Time (s)');
ylabel('Displacement (rad)');
hold on;
grid on;
plot(t2,Y1);
% legend('Linear','Non Linear');
subplot(2,2,2);
% figure(2);
plot(t1,X2);
xlabel('Time (s)');
ylabel('Velocity (rad/s)');
hold on;
grid on;
plot(t2,Y2);
subplot(2,2,3);
plot(X1,X2);
hold on;
plot(Y1,Y2);
xlabel('Displacement (rad)');
ylabel('Velocity (rad/s)');
grid on;
toc;
I'm sorry, i don't understand what you mean by non scalar..
@KLETECH, this is not the statement used in my answer
disp(z) = (12*9.81*L)/((4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2));
According to my answer, it should be like this
z = (12*9.81*L)/((4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2));
disp(z)
right, i've corrected that. thanks.
I am glad to be help!!!
Walter Roberson
on 8 Nov 2020
Edited: Walter Roberson
on 15 Nov 2020
A scalar is any array that holds exactly one piece of information -- not empty, and not two or more values.
The operators * and ^ and / and \ are defined as being matrix operations:
- A*B is algebraic matrix multiplication, inner product of A and B . For A*B to be valid, size(A,2) must be the same as size(B,1) and they cannot have 3 or more dimensions
- A^B is matrix power; when B is a positive integer it is like A*A*A*A...*A for a total of B times, where * here is algebraic matrix multiplication (inner product); the same size restrictions exist as for *
- A/B is like A * pinv(B) but not exactly the same . It is a least-squared operation suitable for solving linear equations. A\B is like pinv(A)*B
These differ from the .* and .^ and ./ and the (rare) .\ operators, all of which are element-by-element operations. C = A.*B is C(J,K) = A(J,K) .* B(J,K) -- multiplication of corresponding locations; likewise for the other operations mentioned.
The matrix operator * acts like .* if one of the operands is scalar instead of matrice. 3*A is A with each element multiplied by 3. The ^ and / operators act like .^ and ./ if both of the operands are scalar.
You have written (12*9.81*L)/((4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2)) . If L happens to be something that is not a scalar, then almost everything you wrote there will work -- the L.^2 would work. For example [2 5]^2 would fail because it would be [2 5]*[2 5] and size() of the second dimension of the first operand is 2 but size() of the first dimension of the second operand is 1 which does not match. [2 5]^2 is not [4 25]. But [2 5].^2 is [4 25] . You wrote your expression nearly all with the dot operators, so nearly all of it will work if L or M happen to be matrices. But you used / and / between two things that are not scalar is going to give you results that you do not expect. You need ./ in that expression if there is a chance that L is not a scalar. If L and M are both something that might not be scalar then L*M should be L.*M to get your code to work the way you expect.
Thank you Walter. Makes more sense now.
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
See Also
on 8 Nov 2020
on 15 Nov 2020
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)