Vector must be same lengths - please help me
Show older comments
Dear sir,
I want to plot the graph between w vs x(:,1) from the following codes. but i got error " vector must be same length" like this. I understood that w and x(:,1) are different vectors. I want both w and x(:,1) are should same length.Please help me regarding this.
Function files:
function xdot = f(t,x)
global m k nu N0 N1 F w
xdot = [x(2); (-k/m)*x(1)-((nu*N0)/m)*sign(x(2))-((nu*N1)/m)*sign(x(2))*sin(w*t)+(F/m)*sin(w*t)];
end
Script file:
clc;
clear all;
global m k nu N0 N1 F w
m = 1; k = 1; nu = 0.2; N0 = 1; N1 = 0.1; F = 0.1;
tspan = [0 1];
int = [0 1];
for w = 1:1:10
[t,x] = ode45(@f,tspan,int);
omega(w) = w;
plot(omega, x(:,2));
end
Regards Devarajan K
1 Comment
John D'Errico
on 1 Jul 2017
Edited: John D'Errico
on 1 Jul 2017
Um, w is NOT a vector. w is a scalar. w is the loop element. Anyway, the plat is between omega, which IS a vector of length 10, and x. What is the size of x? What have you done to ensure that x is the proper size? x is an output from ODE45.
Answers (1)
James Tursa
on 1 Jul 2017
I am not exactly sure how you want things plotted, but maybe this?
%plot(omega, x(:,2));
plot(t,x(:,2)); hold on; grid on
2 Comments
Devarajan K
on 1 Jul 2017
James Tursa
on 1 Jul 2017
Edited: James Tursa
on 1 Jul 2017
Omega is essentially the index of your for loop, a single value. How does it make sense to plot this vs an x(:,2) vector that is 57 elements long? Seems like it makes more sense to plot the time t vs x(:,2) as I have shown, with maybe a legend showing which w goes with which curve?
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!