MATLAB Answers

Not Sure How to Call a Norm from Within a Function

20 views (last 30 days)
Hello,
I'm not sure how to make a plot of a norm that I found inside a function.
Here is my code, which will best help me explain:
clc; clear all;
ti = 0;
tf = 15;
global I11 I22 I33 Mx My Mz w10 w20 w30 eps10 eps20 eps30 eps40 IC K0
I11 = 160;
I22 = 400;
I33 = 400;
Mx = 0;
My = 0;
Mz = 45;
w10 = 2;
w20 = -1;
w30 = 1;
eps10 = 0;
eps20 = 0;
eps30 = 0;
eps40 = 1;
K0 = eps10^2+eps20^2+eps30^2+eps40^2;
IC = [w10 w20 w30 eps10 eps20 eps30 eps40];
[t, y] = ode45(@(t,y) DynEqn1(t,y,I11,I22,I33,Mx,My,Mz), [ti tf], IC);
figure (1)
plot(t,y(:,1:3))
figure (2)
plot(t,y(:,4:7))
figure (3)
plot(t,(norm(y(:,4:7))-K0))
function soln = DynEqn1(t,y,I11,I22,I33,Mx,My,Mz)
w1 = y(1);
w2 = y(2);
w3 = y(3);
eps1 = y(4);
eps2 = y(5);
eps3 = y(6);
eps4 = y(7);
disp(norm(y(4:7)))
w1_dot = (Mx - w2*w3*(I33-I22))/I11;
w2_dot = (My - w1*w3*(I11-I33))/I22;
w3_dot = (Mz - w1*w2*(I22-I11))/I33;
eps1_dot = .5*(w1*eps4-w2*eps3+w3*eps2);
eps2_dot = .5*(w1*eps3+w2*eps4-w3*eps1);
eps3_dot = .5*(-w1*eps2+w2*eps1+w3*eps4);
eps4_dot = -.5*(w1*eps1+w2*eps2+w3*eps3);
soln = [w1_dot; w2_dot; w3_dot; eps1_dot; eps2_dot; eps3_dot; eps4_dot];
end
Basically, if I just display the norm from within the function, I get the values. But I would like to plot those values (minus a set value) versus time, but can't get it to work.
I tried defining the norm from within the function as a variable, K, but that didn't work either.
Any help is appreciated.
Thanks,
Jon

Accepted Answer

Walter Roberson
Walter Roberson on 26 Feb 2021
N = sqrt(sum(y(:,4:7).^2,2));
plot(t, N)
  3 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by