Matlab transpositions from formula

1 view (last 30 days)
Chett Manly on 16 Oct 2021
Answered: Paul on 16 Oct 2021
Having some dramas converting equations to matlab. The code runs but my answers seem wildly out...
Part A uses the formula v=(T^3/2)/a*T+b which i believe translates to V = (T.^3/2)./A.*T+B; in matlab. I think this is right.
Part b uses the formula 1/v=a*(T^-1/2)+b*T^-3/2 which I thought to be in matlab as...
Vtemp = (A2.*TCK.^-1/2)+(B2.*TCK.^-3/2);
V2=1./Vtemp;
Part C runs a formula f(c,t)=(T^3/2)/c(1)*T+c(2) which i am mostly sure is right but not 100%.
clear; close all; clc
%% problem 4
TC = [-20 0 20 40 70 100 200 300 400 500]';
vi = [1.63 1.71 1.82 1.87 2.03 2.17 2.53 2.98 3.32 3.64]';
TCK = TC + 273.15;
%% part a
p = polyfit(1./TCK,vi,1);
B = p(1);
A = p(2);
V = (TCK.^3/2)./A.*TCK+B;
Tspan = linspace(-20,520,10)';
plot(Tspan,V,'bo',Tspan,V,'r-');
grid on
xlabel('Temp')
ylabel('Viscocity')
title('viscosity polyfit part a')
%% part b
p2 = polyfit(1./TCK,vi,1);
B2 = p2(1);
A2 = p2(2);
Vtemp = (A2.*TCK.^-1/2)+(B2.*TCK.^-3/2);
V2=1./Vtemp;
Tspan = linspace(-20,520,10)';
figure; plot(Tspan,V2,'bo',Tspan,V2,'r-');
grid on
xlabel('Temp')
ylabel('Viscocity')
title('viscosity polyfit part b')
%% part c
f = @(c,T) T.^3/2./c(1).*T+c(2);
c0 = [1;1];
c = nlinfit(TCK,vi,f,c0);
V3 = f(c,Tspan);
figure; plot(Tspan,V3,'bo',Tspan,V3,'r-');
grid on
xlabel('Temp')
ylabel('Viscocity')
title('viscosity nlinfit part C')
%% part d
% title('viscosity - polyfit-2 method')
%
% fprintf(' A B method\n')
% fprintf(['-------------------------------','\n'])
% fprintf(' %6.4f %6.4f %-7s\n',A2,B2,'polyfit-2')

Paul on 16 Oct 2021
Maybe you really mean to put all those fractional exponents inside parentheses? For example:
Tck = 4;
Tck.^3/2 % or
ans = 32
Tck.^(3/2) % equivalent to
ans = 8
Tck.^1.5
ans = 8