Getting blank plot in MATLAB
이전 댓글 표시
Hi, could I know where I might be wrong in the following code as Im getting blank graph. The parameter vel and acc are time dependent which in turn Fh is function of time.
function [Fh] = hydro(t)
Cd = 0.6;
Ca = 0.9699;
R = 4.7;
for t=0:0.1:10
vel = compute_wavevelocity(t)
acc = compute_waveacceleration(t)
fun = sym((0.5*997*Cd*2*R*abs(vel)*vel)+(Ca*997*pi*(R^2)*acc)+(pi*(R^2)*997*acc));
Fh(t==0:0.1:10) = eval(int(fun,0,-120));
end
plot(t,Fh), xlabel('time(s)'), ylabel('Fh')
title ('Fh vs Time')
end
댓글 수: 4
madhan ravi
2020년 7월 14일
Satish Jawalageri
2020년 7월 14일
Image Analyst
2020년 7월 14일
Do you really even need symbolics? Or could you do it numerically, something like this:
Cd = 0.6;
Ca = 0.9699;
R = 4.7;
all_t=0:0.1:10
for k = 1 : length(all_t)
t = all_t(k);
vel = compute_wavevelocity(t)
acc = compute_waveacceleration(t)
fun = (0.5*997*Cd*2*R*abs(vel)*vel)+(Ca*997*pi*(R^2)*acc)+(pi*(R^2)*997*acc);
Fh(k) = fun;
end
plot(all_t,Fh), xlabel('time(s)'), ylabel('Fh')
title ('Fh vs Time')
Satish Jawalageri
2020년 7월 15일
답변 (1개)
Cris LaPierre
2020년 7월 14일
This is because t is a scalar. When you plot, it has the value it was assigned on the last loop of the for loop: 10.
Try changing the x input in your plot command:
plot(0:0.1:10,Fh)
댓글 수: 13
Satish Jawalageri
2020년 7월 14일
Cris LaPierre
2020년 7월 14일
Check your function for errors? With what you have provided, I can't help.
Satish Jawalageri
2020년 7월 14일
madhan ravi
2020년 7월 14일
편집: madhan ravi
2020년 7월 14일
Huh? Have you shown what compute_velocity & compute_waveacceleration function is? Are we supposed to read your mind?
Satish Jawalageri
2020년 7월 14일
Cris LaPierre
2020년 7월 14일
It is cyclical, occurring every 2.5 seconds. It appears to be related to your expression for vel and acc. Specifically, cos((k*0)-(omega*t)) and sin((k*0)-(omega*t)). Since k*0 is always zero, and since sin and cos return a value of 0 in increments spaced π apart, I'd say that the result you are observing is to be expected based on the code you have shared.
Check that your equation is correct.
Satish Jawalageri
2020년 7월 14일
편집: Satish Jawalageri
2020년 7월 14일
Satish Jawalageri
2020년 7월 14일
Cris LaPierre
2020년 7월 14일
편집: Cris LaPierre
2020년 7월 14일
This appears to be due to how you are assigning your values to Fh in your for loop. It's likely taking too long, so the result is some values get skipped. Since this is an unconventional way to do it anyway, I suggest updating your code to the following.
function Fh = hydro(t)
Cd = 0.6;
Ca = 0.9699;
R = 4.7;
t=0:0.1:10;
for i = 1:length(t)
vel = compute_wavevelocity(t(i));
acc = compute_waveacceleration(t(i));
fun = sym((0.5*997*Cd*2*R*abs(vel)*vel)+(Ca*997*pi*(R^2)*acc)+(pi*(R^2)*997*acc));
Fh(i,1) = double(int(fun,0,-120));
end
plot(t,Fh), xlabel('time(s)'), ylabel('Fh')
title ('Fh vs Time')
Let me anticipate the next issue.
You have hardcoded your function to run for 10 seconds, meaning that it does not matter what value you call hydro with. You will always get the result for 10 seconds.
Satish Jawalageri
2020년 7월 15일
Satish Jawalageri
2020년 7월 15일
편집: Satish Jawalageri
2020년 7월 15일
Cris LaPierre
2020년 7월 15일
These appear to be new questions. I suggest creating a new post for each.
Satish Jawalageri
2020년 7월 15일
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
