필터 지우기
필터 지우기

I really want to draw the precession of mercury

조회 수: 2 (최근 30일)
Asir Tushar
Asir Tushar 2017년 11월 10일
댓글: Meindert Norg 2018년 1월 9일
clc;clear all;
a=10;
b=5;
lambda=.5;
v1=1/20;
v2=3;
t=0:.1:40;
x(t)=a.^2+b.^2 +a*cos(2*pi*v1*t)*cos(2*pi*v2*t)-lambda*b*sin(2*pi*v1*t)*sin(2*pi*v2*t);
y(t)=a.^2+b.^2 +a*sin(2*pi*v1*t)*cos(2*pi*v2*t)-lambda.*b*cos(2*pi*v1*t)*sin(2*pi*v2*t);
plot(x(t),y(t))
This is my program. but it's not running cause there is a dimension mismatch. can anybody tell me what did i do wrong?
  댓글 수: 1
Meindert Norg
Meindert Norg 2018년 1월 9일
In case you have not figured it out in the mean time:
  • Remove the element-wise ".^2"
a^2+b^2
  • add element wise multiplication for the vectors where 't' is used:
a*cos(2*pi*v1*t).*cos(2*pi*v2*t)
  • fix the way you plot it:
plot(x,y)
Here is the result:
clc;clear all;
a=10;
b=5;
lambda=.5;
v1=1/20;
v2=3;
t=0:.1:40;
x=a^2+b^2 + a*cos(2*pi*v1*t).*cos(2*pi*v2*t)-
lambda*b*sin(2*pi*v1*t).*sin(2*pi*v2*t);
y=a^2+b^2 + a*sin(2*pi*v1*t).*cos(2*pi*v2*t)-
lambda.*b*cos(2*pi*v1*t).*sin(2*pi*v2*t);
plot(x,y)

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2017년 11월 10일
As the error message says
x(t)
is only valid if t is a real positive integer.
The simplest way to solve your problem is to get rid of the indexing of x and y which is completely meaningless
x = a^2 + b^2 + a*cos(2*pi*v1*t) .* cos(2*pi*v2*t) - ...
y = a^2 + b^2 + ...
plot(x, y)

추가 답변 (1개)

M
M 2017년 11월 10일
편집: M 2017년 11월 10일
First :
a.^2
the dot is useless as a is a scalar
but I think you should add a dot between the cos multiplication :
a*cos(2*pi*v1*t).*cos(2*pi*v2*t)
otherwise you will get an error.
Then, if you start at t=0, you will get an error as you're trying to acces
x(t)=x(0)
but Matlab indices should be positive integers.
  댓글 수: 1
Asir Tushar
Asir Tushar 2017년 11월 10일
x(t)=a^2+b^2 +a*cos(2*pi*v1*t).*cos(2*pi*v2*t)-lambda*b*sin(2*pi*v1*t).*sin(2*pi*v2*t);
y(t)=a^2+b^2 +a*sin(2*pi*v1*t).*cos(2*pi*v2*t)-lambda*b*cos(2*pi*v1*t).*sin(2*pi*v2*t);
i corrected the code like you said. still this message is showing for these two lines "Subscript indices must either be real positive integers or logicals.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by