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 Comments

Okay, thanks. I will check it.
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')
Thanks for your reply.
I will check this but here Fh is integration of fun.

Sign in to comment.

Answers (1)

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 Comments

Oh yeah! Perfect.
Thanks.
And one more, I am getting zero value in vel and acc corresponding to few values of t (which is not correct correct in this case). Could I know what might be the issue?
Thanks.
Check your function for errors? With what you have provided, I can't help.
Oky, no worries. Thanks.
madhan ravi
madhan ravi on 14 Jul 2020
Edited: madhan ravi on 14 Jul 2020
Huh? Have you shown what compute_velocity & compute_waveacceleration function is? Are we supposed to read your mind?
Sorry! Code for both velocity and acceleration.
Velocity:
function velocity = compute_wavevelocity(t)
T = 10;
WN = (9.8*(T^2))/(2*pi());
k = (2*pi())/WN;
omega = (2*pi())/T;
y = [320;310;300;290;280;270;260;250;240;230;220;210;200];
d = 320;
H = 4;
vel = (omega*H/2)* ((cosh(k*y))/(sinh(k*d)))* cos((k*0)-(omega*t));
velocity = mean(vel,1);
end
Acceleration:
function acceleration = compute_waveacceleration(t)
T = 10;
WN = (9.8*(T^2))/(2*pi());
k = (2*pi())/WN;
omega = (2*pi())/T;
y = [320;310;300;290;280;270;260;250;240;230;220;210;200];
d = 320;
H = 4;
acc = ((omega^2)*H/2)* ((cosh(k*y))/(sinh(k*d)))* sin((k*0)-(omega*t));
acceleration = mean(acc,1);
end
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
Satish Jawalageri on 14 Jul 2020
Edited: Satish Jawalageri on 14 Jul 2020
Thanks for your reply.
I got that correct, but when I run hydro(t) I am getting proper values of Fh till half time (ie here till 5sec) and then, few values get to zero, i dont know why. Further, I checked fun values which are correct for given time. So Fh(t==0:0.1:10) = eval(int(fun,0,-120)); Is it correct to mention like this?
No, it is the same.
I have attached the plot which i got when run the code.
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.
Thank you very much. I got it.
Could I know how to run this state space model and ode45 function and Is it correct what I have done. Here, F1 and F2 are fuction of Fh.
State space model: (Output.m)
function [sdot] = Output(t,x)
Ze = zeros(3);
I = eye(3);
M = [8070000,0,-629468070;0,8070000,112980;-629468070,112980,6.800000000000000e+10];
Ad = [8.053095218400001e+06,0,-4.831857131040000e+08;0,2.167940435676214e+05,0;-4.831857131040000e+08,0,3.865485704832000e+10];
C = [0,0,0;0,3.241885080000000e+05,0;0,0,1.301151158327999e+09];
Cm = [4.12e+04,0,-2.82e+06;0,1.19e+04,0;-2.82e6,0,3.11e+08];
MA = M+Ad;
Q = C+Cm;
Fwt = 2.793977550000000e+04; %Wind force on turbine tower
Fg = -79086000; %Gravitational force
Fbuoy = 7.844814740000000e+07; %Buoyancy force
Fp = 2.712318560000001e+06; %Heave force
Fmx = -334731.8545;
Fmz = -3517000;
t = 0:0.1:10
for i = 1:length(t)
Fh = hydro(t(i))
F1(1,i) = Fmx+27939.6+6.5*10^5+Fh(1,i);
F2(1,i) = Fmz+Fg+Fbuoy+Fp;
F3(1,i) = -112510430.2+3.44*10^6+266.5*10^5+(Fh(1,i)*18);
A = [Ze, I; -inv(MA)*Q, Ze];
B = [Ze; inv(MA)];
F = [F1;F2;F3];
Svec = [x(1);x(2);x(3);x(4);x(5);x(6)];
%
sdot(1,i) = A*Svec + B*F;
end
end
ODE45:
%Initial Condition
x(10) = 0; x(20) = 0; x(30) = 0;
x(40) = 0; x(50) = 0; x(60) = 0;
IC = [x(10);x(20);x(30);x(40);x(50);x(60)];
%time span
t0 = 0; tf = 10;
tspan = t0:0.1:tf;
[t, x] = ode45(@Output,tspan,IC);
IC = [IC;x(end)];
%x(1) = x(:,1);
%x(2) = x(:,2);
%x(3) = x(:,3);
%x(4) = x(:,4);
%x(5) = x(:,5);
%x(6) = x(:,6);
%Plot Results
figure(1), clf
plot(t,x(:,1)), xlabel('time(s)'), ylabel('surge(m)')
title ('Surge vs Time')
These appear to be new questions. I suggest creating a new post for each.
Sure, I will.
Thanks.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2019b

Asked:

on 14 Jul 2020

Commented:

on 15 Jul 2020

Community Treasure Hunt

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

Start Hunting!