Vector must be same lengths - please help me

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 个评论

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.

请先登录,再进行评论。

回答(1 个)

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 个评论

Thanks for your reply. I want to plot omega vs x(:,2).
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?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by